合并两个Datatable 参见:.NET Framework 类库 DataTable.Merge 方法 将指定的 DataTable 与当前的 DataTable 合并。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 楼主可以参见如下MSDN的例子,以便应用Merge的方法:private static void DemonstrateMergeTable(){ DataTable table1 = new DataTable("Items"); // Add columns DataColumn column1 = new DataColumn("id", typeof(System.Int32)); DataColumn column2 = new DataColumn("item", typeof(System.Int32)); table1.Columns.Add(column1); table1.Columns.Add(column2); // Set the primary key column. table1.PrimaryKey = new DataColumn[] { column1 }; // Add RowChanged event handler for the table. table1.RowChanged += new System.Data.DataRowChangeEventHandler(Row_Changed); // Add some rows. DataRow row; for (int i = 0; i <= 3; 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 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; table2.Rows.Add(row); row = table2.NewRow(); row["id"] = 12; row["item"] = 555; table2.Rows.Add(row); row = table2.NewRow(); row["id"] = 13; row["item"] = 665; table2.Rows.Add(row); // Merge table2 into the table1. Console.WriteLine("Merging"); table1.Merge(table2); PrintValues(table1, "Merged With table1");}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(); }} using System;using System.Data;class Test{ static void Main() { DataTable table = new DataTable(); table.Columns.Add("num", typeof(int)); table.Rows.Add(10); table.Rows.Add(20); DataTable table2 = new DataTable(); table2.Columns.Add("num", typeof(int)); table2.Rows.Add(10); table2.Rows.Add(50); int max = GetMaxNumber(table, table2); Console.WriteLine(max);//50 Console.ReadLine(); } static int GetMaxNumber(DataTable tb1, DataTable tb2) { tb1.Merge(tb2); int maxnum = 0; foreach (DataRow row in tb1.Rows) { int tmp = Convert.ToInt32(row[0]); if (maxnum >= tmp) { continue; } maxnum = tmp; } return maxnum; }} 这个就行http://msdn.microsoft.com/zh-cn/library/wkk7s5zk(VS.80).aspx label控件的高度自动适应 关于在ASP.NET中的 SelectCommand中使用函数的问题 B/S 输出模板Excel 数据库代码怎么写?? 问个新闻分页的例子,20分 不够在加 ASP.net(c#)高手进来(代码优化的问题) 关于用户控件中图片的路径 AspNetPager控件做翻页,当从其他页返回到第一页时,出错,查询条件丢失,如何保存查询条件 asp.net中,怎样控制textbox中输入的回车? ASP.NET中能不能$.ajax 来调用后台方法,如果能请给出详细例子,谢谢啊 aspx页面引用js文件问题 各位兄台,很着急的一个问题(关于用户控件)
private static void DemonstrateMergeTable()
{
DataTable table1 = new DataTable("Items"); // Add columns
DataColumn column1 = new DataColumn("id", typeof(System.Int32));
DataColumn column2 = new DataColumn("item", typeof(System.Int32));
table1.Columns.Add(column1);
table1.Columns.Add(column2); // Set the primary key column.
table1.PrimaryKey = new DataColumn[] { column1 }; // Add RowChanged event handler for the table.
table1.RowChanged +=
new System.Data.DataRowChangeEventHandler(Row_Changed); // Add some rows.
DataRow row;
for (int i = 0; i <= 3; 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 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;
table2.Rows.Add(row); row = table2.NewRow();
row["id"] = 12;
row["item"] = 555;
table2.Rows.Add(row); row = table2.NewRow();
row["id"] = 13;
row["item"] = 665;
table2.Rows.Add(row); // Merge table2 into the table1.
Console.WriteLine("Merging");
table1.Merge(table2);
PrintValues(table1, "Merged With table1");}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();
}
}
using System.Data;
class Test
{ static void Main()
{
DataTable table = new DataTable();
table.Columns.Add("num", typeof(int));
table.Rows.Add(10);
table.Rows.Add(20);
DataTable table2 = new DataTable();
table2.Columns.Add("num", typeof(int));
table2.Rows.Add(10);
table2.Rows.Add(50);
int max = GetMaxNumber(table, table2);
Console.WriteLine(max);//50
Console.ReadLine();
}
static int GetMaxNumber(DataTable tb1, DataTable tb2)
{
tb1.Merge(tb2);
int maxnum = 0;
foreach (DataRow row in tb1.Rows)
{
int tmp = Convert.ToInt32(row[0]);
if (maxnum >= tmp)
{
continue;
}
maxnum = tmp;
}
return maxnum; }
}
http://msdn.microsoft.com/zh-cn/library/wkk7s5zk(VS.80).aspx