|
 |
|
ASP.Net สอบถามวิธีการทำ Crystal Report จากข้อมูลใน GridView ครับ ^ ^" |
|
 |
|
|
 |
 |
|
สอบถามวิธีการทำCrystal Report จากข้อมูลใน GridView ครับ ^ ^"
จากโค้ด
Code (C#)
using System;
using System.Data;
using System.Web.UI.WebControls;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGridview();
BindSecondGrid();
}
}
protected void BindGridview()
{
DataTable dt = new DataTable();
dt.Columns.Add("UserId", typeof(Int32));
dt.Columns.Add("UserName", typeof(string));
dt.Columns.Add("Education", typeof(string));
dt.Columns.Add("Location", typeof(string));
DataRow dtrow = dt.NewRow();
dtrow["UserId"] = 1;
dtrow["UserName"] = "SureshDasari";
dtrow["Education"] = "B.Tech";
dtrow["Location"] = "Chennai";
dt.Rows.Add(dtrow);
dtrow = dt.NewRow();
dtrow["UserId"] = 2;
dtrow["UserName"] = "MadhavSai";
dtrow["Education"] = "MBA";
dtrow["Location"] = "Nagpur";
dt.Rows.Add(dtrow);
dtrow = dt.NewRow();
dtrow["UserId"] = 3;
dtrow["UserName"] = "MaheshDasari";
dtrow["Education"] = "B.Tech";
dtrow["Location"] = "Nuzividu";
dt.Rows.Add(dtrow);
gvDetails.DataSource = dt;
gvDetails.DataBind();
}
protected void chkSelect_CheckChanged(object sender, EventArgs e)
{
GetSelectedRows();
BindSecondGrid();
}
protected void BindSecondGrid()
{
DataTable dt = (DataTable)ViewState["GetRecords"];
gvTranferRows.DataSource = dt;
gvTranferRows.DataBind();
}
private void GetSelectedRows()
{
DataTable dt;
if (ViewState["GetRecords"] != null)
{
dt = (DataTable)ViewState["GetRecords"];
}
else
{
dt = CreateTable();
}
for (int i = 0; i < gvDetails.Rows.Count; i++)
{
CheckBox chk = (CheckBox)gvDetails.Rows[i].Cells[0].FindControl("chkSelect");
if (chk.Checked)
{
dt = AddGridRow(gvDetails.Rows[i], dt);
}
else
{
dt = RemoveRow(gvDetails.Rows[i], dt);
}
}
ViewState["GetRecords"] = dt;
}
private DataTable CreateTable()
{
DataTable dt = new DataTable();
dt.Columns.Add("UserId");
dt.Columns.Add("UserName");
dt.Columns.Add("Education");
dt.Columns.Add("Location");
dt.AcceptChanges();
return dt;
}
private DataTable AddGridRow(GridViewRow gvRow, DataTable dt)
{
DataRow[] dr = dt.Select("UserId = '" + gvRow.Cells[1].Text + "'");
if (dr.Length <= 0)
{
dt.Rows.Add();
int rowscount = dt.Rows.Count - 1;
dt.Rows[rowscount]["UserId"] = gvRow.Cells[1].Text;
dt.Rows[rowscount]["UserName"] = gvRow.Cells[2].Text;
dt.Rows[rowscount]["Education"] = gvRow.Cells[3].Text;
dt.Rows[rowscount]["Location"] = gvRow.Cells[4].Text;
dt.AcceptChanges();
}
return dt;
}
private DataTable RemoveRow(GridViewRow gvRow, DataTable dt)
{
DataRow[] dr = dt.Select("UserId = '" + gvRow.Cells[1].Text + "'");
if (dr.Length > 0)
{
dt.Rows.Remove(dr[0]);
dt.AcceptChanges();
}
return dt;
}

การทำงานจะเป็นการคลิกเลือกที่ช่อง CheckBox ของ GridView อันที่หนึ่งที่ชื่อ gvDetails เพื่อส่งค่าไปปรากฏในGridView อีกอันที่ชื่อ gvDetails ในขั้นตอนนี้ผมอยากทราบว่าเราสามารถเอาข้อมูลใน gvDetails นี่ไปทำเป็น crystal report ได้ไหมครับ พอจะบอกวิธีการหรือตัวอย่างในศึกษาได้ไหมครับ ^ ^"
เครดิต :http:// http://www.aspdotnet-suresh.com/2013/05/move-selected-gridview-rows-to-another.html
เป็นตัวอย่างที่ค้นเจอในอินเตอร์เน็ตน่ะครับ ซึ่งมีการทำงานคล้ายกับงานของผมเอง แต่งานของผมมีส่วนที่ไม่อาจเอามาแสดงได้ จึงเอามาเป็นตัวอย่างน่ะครับ ^ ^"
Tag : ASP.NET VS 2012 (.NET 4.x)

|
ประวัติการแก้ไข 2013-09-03 00:14:48
|
 |
 |
 |
 |
Date :
2013-09-03 00:12:05 |
By :
Spada_555 |
View :
1208 |
Reply :
2 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ทำเป็น DataTable จะได้ครับ คือทำอะไรก็ตามเอาข้อมูลทีได้ไปอยู่ในรูปแบบของ DataTable
การสร้าง Crystal Report กับ DataSet หรือ DataTable (VB.NET,C#)
ตัวอย่างการทำ DataTable
Code (C#)
DataTable dt = new DataTable();
DataRow dr;
//*** Column ***//
dt.Columns.Add("CustomerID");
dt.Columns.Add("Name");
dt.Columns.Add("Email");
dt.Columns.Add("CountryCode");
dt.Columns.Add("Budget");
dt.Columns.Add("Used");
//*** Rows ***//
dr = dt.NewRow();
dr["CustomerID"] = "C001";
dr["Name"] = "Win Weerachai";
dr["Email"] = "[email protected]";
dr["CountryCode"] = "TH";
dr["Budget"] = "1000000";
dr["Used"] = "600000";
dt.Rows.Add(dr);
//*** Rows ***//
dr = dt.NewRow();
dr["CustomerID"] = "C002";
dr["Name"] = "John Smith";
dr["Email"] = "[email protected]";
dr["CountryCode"] = "EN";
dr["Budget"] = "2000000";
dr["Used"] = "800000";
dt.Rows.Add(dr);
//*** Rows ***//
dr = dt.NewRow();
dr["CustomerID"] = "C003";
dr["Name"] = "Jame Born";
dr["Email"] = "[email protected]";
dr["CountryCode"] = "US";
dr["Budget"] = "3000000";
dr["Used"] = "600000";
dt.Rows.Add(dr);
//*** Rows ***//
dr = dt.NewRow();
dr["CustomerID"] = "C004";
dr["Name"] = "Chalee Angel";
dr["Email"] = "[email protected]";
dr["CountryCode"] = "US";
dr["Budget"] = "4000000";
dr["Used"] = "100000";
dt.Rows.Add(dr);
|
 |
 |
 |
 |
Date :
2013-09-03 09:35:06 |
By :
mr.win |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณมากครับ
|
 |
 |
 |
 |
Date :
2013-09-04 20:00:30 |
By :
Spada_555 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 04
|