There are certain scenarios where you have data in 2 different data tables and you want to merge the data of these data tables into one of them. In this article, we will see how this can be done using C# in very simple steps. The article assumes that the number of columns are the same in both of the data tables.
The DataTable class of the .Net library provides a very easy method to do it. The method is known as ImportRow(). As the name suggests, this method imports a row from one data table into another data table. To copy all rows from one data table into another, you can use the following code:
- DataTable table1 = GetData(); //get Rows in 1st Data Table
- DataTable table2 = GetData(); //get Rows in 2nd Data Table
- foreach (DataRow item in table2.Rows) //Iterate through each row of 2nd data table
- {
- table1.ImportRow(item); //Import the row in 1st data table
- }
I hope you like this! Keep learning and sharing! Cheers!

Suhas KursePosted Aug 26, 2019, 10:16 PM
Hello I am Microsoft unit testing library. I am comparing a JSON file array collection to a database. I have thousand plus collections. I have the sql query which uses the primary key to match the data in JSON file. I want to fetch 250 rows of data per sql call. Can you should how I can convert the datatable into a dictionary so I can compare elements one by one
SubashPosted Sep 10, 2016, 10:17 AM
Nice share
Nitesh KejriwalPosted Oct 28, 2015, 9:46 PM
Thanks
Mukesh KumarPosted Oct 26, 2015, 1:39 AM
Good Job