有時我們知道了兩個DataTable1和DataTable2,我們希望將它們合并為一個新的DataTable(下面的例子中命名為了newDataTable),這就分為兩種情況:DataTable1和DataTable2結(jié)構(gòu)相同、DataTable1和DataTable2結(jié)構(gòu)不同,下面分別介紹怎么進行合并。
一、DataTable1和DataTable2結(jié)構(gòu)相同的情況,結(jié)構(gòu)相同我們只需要把兩者的數(shù)據(jù)羅列到一塊就可以了
首先初始化相同結(jié)構(gòu)DataTable(測試用的)
void InitDataTable1(DataTable dt) { dt.Columns.Add("student_no"); dt.Columns.Add("student_name"); dt.Rows.Add("001", "June"); dt.Rows.Add("002", "zhang"); dt.Rows.Add("003", "jun"); } void InitDataTable2(DataTable dt) { dt.Columns.Add("student_no"); dt.Columns.Add("student_name"); dt.Rows.Add("111", "ABC"); dt.Rows.Add("222", "XYZ"); dt.Rows.Add("333", "OPQ"); }
合并方法1:用Rows.Add方法
DataTable newDataTable = DataTable1.Clone(); object[] obj = new object[newDataTable.Columns.Count]; //添加DataTable1的數(shù)據(jù) for (int i = 0; i < DataTable1.Rows.Count; i++) { DataTable1.Rows[i].ItemArray.CopyTo(obj, 0); newDataTable.Rows.Add(obj); } //添加DataTable2的數(shù)據(jù) for (int i = 0; i < DataTable2.Rows.Count; i++) { DataTable2.Rows[i].ItemArray.CopyTo(obj, 0); newDataTable.Rows.Add(obj); }
合并方法2:用DataTable.ImportRow方法
//拷貝DataTable1的結(jié)構(gòu)和數(shù)據(jù) DataTable newDataTable = DataTable1.Copy(); //添加DataTable2的數(shù)據(jù) foreach (DataRow dr in DataTable2.Rows) { newDataTable.ImportRow(dr); }
其實添加DataTable1的結(jié)構(gòu)和數(shù)據(jù)有兩種方法
//克隆DataTable1的結(jié)構(gòu) DataTable newDataTable = DataTable1.Clone(); object[] obj = new object[newDataTable.Columns.Count]; //添加DataTable1的數(shù)據(jù) for (int i = 0; i < DataTable1.Rows.Count; i++) { DataTable1.Rows[i].ItemArray.CopyTo(obj, 0); newDataTable.Rows.Add(obj); }
或者直接用自帶的Copy方法 DataTable newDataTable = DataTable1.Copy();
二、DataTable1和DataTable2結(jié)構(gòu)不同相同的情況,我們可以先向新表中添加DataTable1的數(shù)據(jù),然后再向每行的后面添加添加DataTable2的數(shù)據(jù),注意兩者的行數(shù)不一定相同。
首先初始化相同結(jié)構(gòu)DataTable(測試用的)
void InitDataTable1(DataTable dt) { dt.Columns.Add("student_no1"); dt.Columns.Add("student_name1"); dt.Rows.Add("001", "June"); dt.Rows.Add("002", "zhang"); //dt.Rows.Add("003", "jun"); } void InitDataTable2(DataTable dt) { dt.Columns.Add("student_no2"); dt.Columns.Add("student_name2"); dt.Rows.Add("111", "ABC"); dt.Rows.Add("222", "XYZ"); dt.Rows.Add("222", "ASD"); }
(1)先添加第一個表,再添加第二個表
/// <summary> /// 將兩個列不同(結(jié)構(gòu)不同)的DataTable合并成一個新的DataTable /// </summary> /// <param name="DataTable1">表1</param> /// <param name="DataTable2">表2</param> /// <param name="DTName">合并后新的表名</param> /// <returns>合并后的新表</returns> private DataTable UniteDataTable(DataTable DataTable1, DataTable DataTable2, string DTName) { //克隆DataTable1的結(jié)構(gòu) DataTable newDataTable = DataTable1.Clone(); for (int i = 0; i < DataTable2.Columns.Count; i++) { //再向新表中加入DataTable2的列結(jié)構(gòu) newDataTable.Columns.Add(DataTable2.Columns[i].ColumnName); } object[] obj = new object[newDataTable.Columns.Count]; //添加DataTable1的數(shù)據(jù) for (int i = 0; i < DataTable1.Rows.Count; i++) { DataTable1.Rows[i].ItemArray.CopyTo(obj, 0); newDataTable.Rows.Add(obj); } if (DataTable1.Rows.Count >= DataTable2.Rows.Count) { for (int i = 0; i < DataTable2.Rows.Count; i++) { for (int j = 0; j < DataTable2.Columns.Count; j++) { newDataTable.Rows[i][j + DataTable1.Columns.Count] = DataTable2.Rows[i][j].ToString(); } } } else { DataRow dr3; //向新表中添加多出的幾行 for (int i = 0; i < DataTable2.Rows.Count - DataTable1.Rows.Count; i++) { dr3 = newDataTable.NewRow(); newDataTable.Rows.Add(dr3); } for (int i = 0; i < DataTable2.Rows.Count; i++) { for (int j = 0; j < DataTable2.Columns.Count; j++) { newDataTable.Rows[i][j + DataTable1.Columns.Count] = DataTable2.Rows[i][j].ToString(); } } } newDataTable.TableName = DTName; //設(shè)置DT的名字 return newDataTable;
(2)先添加行數(shù)多的表。其實我們也可以先判斷哪個表的行數(shù)多,就先添加哪個表,然后再添加行少的表就可以了。
/// <summary> /// 將兩個列不同(結(jié)構(gòu)不同)的DataTable合并成一個新的DataTable /// </summary> /// <param name="DataTable1">表1</param> /// <param name="DataTable2">表2</param> /// <param name="DTName">合并后新的表名</param> /// <returns>合并后的新表</returns> private DataTable UniteDataTable2(DataTable DataTable1, DataTable DataTable2, string DTName) { DataTable newDataTable = new DataTable(); if (DataTable1.Rows.Count > DataTable2.Rows.Count) { newDataTable = FillData(DataTable1, DataTable2); } else { newDataTable = FillData(DataTable2, DataTable1); } newDataTable.TableName = DTName; //設(shè)置DT的名字 return newDataTable; } private DataTable FillData(DataTable dt1, DataTable dt2) { //克隆DataTable1的結(jié)構(gòu) DataTable newDataTable = dt1.Clone(); for (int i = 0; i < dt2.Columns.Count; i++) { //再向新表中加入DataTable2的列結(jié)構(gòu) newDataTable.Columns.Add(dt2.Columns[i].ColumnName); } object[] obj = new object[newDataTable.Columns.Count]; //添加DataTable1的數(shù)據(jù) for (int i = 0; i < dt1.Rows.Count; i++) { dt1.Rows[i].ItemArray.CopyTo(obj, 0); newDataTable.Rows.Add(obj); } for (int i = 0; i < dt2.Rows.Count; i++) { for (int j = 0; j < dt2.Columns.Count; j++) { newDataTable.Rows[i][j + dt1.Columns.Count] = dt2.Rows[i][j].ToString(); } } return newDataTable; }