-
How to get value after merging two columns from two different DataTables?
over 9 years ago
-
over 9 years ago
As per my understanding you want to merge two column values from two different DataTables into a single DataTable.
In a simplest manner you can iterate through each row of the two DataTables to be merged and then save merged values into a result DataTables
Please have a look at the sample code below, in case you have any problem, please let me know.
//Inserting value in the first DataTable dt1 DataTable dt1 = new DataTable(); dt1.Columns.Add("FirstName"); DataRow dr = dt1.NewRow(); dr["FirstName"] = "FirstName_1"; dt1.Rows.Add(dr); dr = dt1.NewRow(); dr["FirstName"] = "FirstName_2"; dt1.Rows.Add(dr); //Inserting value in the second DataTable dt2 DataTable dt2 = new DataTable(); dt2.Columns.Add("LastName"); DataRow dr1 = dt2.NewRow(); dr1["LastName"] = "LastName_1"; dt2.Rows.Add(dr1); dr1 = dt2.NewRow(); dr1["LastName"] = "LastName_2"; dt2.Rows.Add(dr1); //Merging the values of two columns into another DataTable dt3 DataTable dt3 = new DataTable(); dt3.Columns.Add("Name"); DataRow dr3; for (int i = 0; i < dt1.Rows.Count; i++) { dr3 = dt3.NewRow(); dr3["Name"] = dt1.Rows[i]["FirstName"].ToString() + " " + dt2.Rows[i] ["LastName"].ToString(); dt3.Rows.InsertAt(dr3, i); }
1 Answer(s)