日期:2014-05-18 浏览次数:20555 次
DataTable table1 = new DataTable();
table1.Columns.Add("a1");
table1.Columns.Add("a2");
table1.Columns.Add("a3");
table1.Columns.Add("a4");
table1.Columns.Add("a5");
table1.Columns.Add("a6");
table1.Rows.Add(new object[] { 1,2,3,4,5,6});
DataTable table2 = new DataTable();
table2.Columns.Add("b1");
table2.Columns.Add("b2");
table2.Rows.Add(new object[] { "b1", "b2" });
DataTable table3 = new DataTable();
foreach (DataColumn c in table1.Columns)
{
table3.Columns.Add(c.ColumnName, c.DataType);//先添加的table1的Columns
}
foreach (DataColumn c in table2.Columns)
{
table3.Columns.Add(c.ColumnName, c.DataType);//后添加的table2的Columns
}
for (int i = 0; i < table1.Rows.Count || i < table2.Rows.Count; i++)
{
//取值的时候先取table1,再取table2
object[] o = table1.Rows[i] == null ? new object[table1.Columns.Count] : table1.Rows[i].ItemArray;
object[] o2 = table2.Rows[i] == null ? new object[table2.Columns.Count] : table2.Rows[i].ItemArray;
object[] o3 = new object[o.Length + o2.Length];
o.CopyTo(o3,0);//copy的时候先copy table1
o2.CopyTo(o3, o.Length);//再copy table2(与前面的列对应)
table3.Rows.Add(o3);
}