|
|
|
รบกวนสอบถามเรื่อง การรวมค่า Gridview 2 Gridview เพื่อมาแสดงใน Gridview 3 |
|
|
|
|
|
|
|
คุณก็รวมด้วย DataTable ก็ได้ครับ เมื่อได้ DataTable ตัวใหม่ก็โยน DataSource ได้เลยครับ
ตัวอย่างการสร้าง 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);
ได้ค่า dt เอาไปโยนใน DataSource ได้เลยครับ
|
|
|
|
|
Date :
2011-08-04 13:01:32 |
By :
webmaster |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
หรือกรณีที่มี DataTable 2 ตัวสามารถนำมา Merge ร่วมกันได้ครับ
Code (C#)
private static void DemonstrateMergeTable()
{
DataTable table1 = new DataTable("Items");
// Add columns
DataColumn idColumn = new DataColumn("id", typeof(System.Int32));
DataColumn itemColumn = new DataColumn("item", typeof(System.Int32));
table1.Columns.Add(idColumn);
table1.Columns.Add(itemColumn);
// Set the primary key column.
table1.PrimaryKey = new DataColumn[] { idColumn };
// Add RowChanged event handler for the table.
table1.RowChanged += new
System.Data.DataRowChangeEventHandler(Row_Changed);
// Add ten rows.
DataRow row;
for (int i = 0; i <= 9; i++)
{
row = table1.NewRow();
row["id"] = i;
row["item"] = i;
table1.Rows.Add(row);
}
// Accept changes.
table1.AcceptChanges();
PrintValues(table1, "Original values");
// Create a second DataTable identical to the first.
DataTable table2 = table1.Clone();
// Add column to the second column, so that the
// schemas no longer match.
table2.Columns.Add("newColumn", typeof(System.String));
// Add three rows. Note that the id column can't be the
// same as existing rows in the original table.
row = table2.NewRow();
row["id"] = 14;
row["item"] = 774;
row["newColumn"] = "new column 1";
table2.Rows.Add(row);
row = table2.NewRow();
row["id"] = 12;
row["item"] = 555;
row["newColumn"] = "new column 2";
table2.Rows.Add(row);
row = table2.NewRow();
row["id"] = 13;
row["item"] = 665;
row["newColumn"] = "new column 3";
table2.Rows.Add(row);
// Merge table2 into the table1.
Console.WriteLine("Merging");
table1.Merge(table2, false, MissingSchemaAction.Add);
PrintValues(table1, "Merged With table1, schema added");
}
private static void Row_Changed(object sender,
DataRowChangeEventArgs e)
{
Console.WriteLine("Row changed {0}\t{1}", e.Action,
e.Row.ItemArray[0]);
}
private static void PrintValues(DataTable table, string label)
{
// Display the values in the supplied DataTable:
Console.WriteLine(label);
foreach (DataRow row in table.Rows)
{
foreach (DataColumn col in table.Columns)
{
Console.Write("\t " + row[col].ToString());
}
Console.WriteLine();
}
}
http://msdn.microsoft.com/en-us/library/system.data.datatable.merge.aspx
|
|
|
|
|
Date :
2011-08-04 13:04:18 |
By :
webmaster |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ถ้าเป็นในกรณีที่ Gridview 1 กับ Gridview 2 มีจำนวน column ไม่เท่ากันล่ะคะ
|
|
|
|
|
Date :
2011-08-04 14:03:38 |
By :
m |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
พอจะมีตัวอย่างมั้ยคะ หรือว่าช่วยแนะนำวิธีการทำให้หน่อย
รบกวนด้วยนะคะ
|
|
|
|
|
Date :
2011-08-06 08:12:41 |
By :
m |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 03
|