Hi All
I want to merge Two Datatables to one
like this
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Jisny AvPosted Sep 12, 2016, 12:23 AM
table1.Columns.Add("M2");
table1.Columns.Add("M3");
DataRow row1 = table1.NewRow();
row1["M1"] = "100";
row1["M2"] = "120";
row1["M3"] = "322";
table1.Rows.Add(row1);
DataRow row2 = table1.NewRow();
row2["M1"] = "200";
row2["M2"] = "350";
row2["M3"] = "542";
table1.Rows.Add(row2);
DataRow row3 = table1.NewRow();
row3["M1"] = "300";
row3["M2"] = "120";
row3["M3"] = "544";
table1.Rows.Add(row3);
DataTable table2 = new DataTable();
table2.Columns.Add("ID");
table2.Columns.Add("Name_student");
DataRow row11 = table2.NewRow();
row11["ID"] = "1";
row11["Name_student"] = "Mohammed";
table2.Rows.Add(row11);
DataRow row22 = table2.NewRow();
row22["ID"] = "2";
row22["Name_student"] = "Ali";
table2.Rows.Add(row22);
DataRow row33 = table2.NewRow();
row33["ID"] = "3";
row33["Name_student"] = "Fahe";
table2.Rows.Add(row33);
DataTable table3 = new DataTable();
table3.Columns.Add("M1");
table3.Columns.Add("M2");
table3.Columns.Add("M3");
table3.Columns.Add("ID");
table3.Columns.Add("Name_student");
for (int i = 0; i < table1.Rows.Count; i++)
{
DataRow row111 = table3.NewRow();
row111["M1"] = table1.Rows[i]["M1"].ToString();
row111["M2"] = table1.Rows[i]["M2"].ToString();
row111["M3"] = table1.Rows[i]["M3"].ToString();
row111["ID"] = table2.Rows[i]["ID"].ToString();
row111["Name_student"] = table2.Rows[i]["Name_student"].ToString();
table3.Rows.Add(row111);
}
Bikesh SrivastavaPosted Sep 10, 2016, 3:56 PM
Ramesh PalaniappanPosted Sep 10, 2016, 9:36 AM
Midhun TpPosted Sep 2, 2016, 1:13 PM
http://stackoverflow.com/questions/12278978/combining-n-datatables-into-a-single-datatable
lamasat lamasatPosted Sep 2, 2016, 1:00 PM
Manas MohapatraPosted Sep 2, 2016, 1:00 PM
Midhun TpPosted Sep 2, 2016, 12:36 PM
Please check below links-
http://www.c-sharpcorner.com/UploadFile/0c1bb2/merging-multiple-datatables-into-single-datatable-using-asp/
http://www.itdeveloperzone.com/2012/04/merge-2-datatables-in-c.html?m=1