C# - Add/Copy Rows from One Datatable to Another Datatable

Introduction:

Here I will explain simple
code snippet in c# to add rows from one table to another table in vb.net, asp.net or copy rows from one datatable to another datatable in  c#, vb.net.

Description:

In Previous posts I explained using statement example in c#, Get all files from folder and subfolders in c#, Export webpage with images to pdf in asp.net and many articles relating to
c#, vb.net, Asp.net, Gridview, SQL Server, Ajax, JavaScript etc. Now I will explain how to add rows from one table to another table in c#, vb.net, asp.net.  

To add rows from one table to another table we need to use ImportRow function like as shown below

C# Code

DataTable _dt=new DataTable();
_dt = _ds.Tables[0];
DataTable dt1 = ds1.Tables[0];
for (int i = 0; i < dt1.Rows.Count; i++)
{
_dt.ImportRow(dt1.Rows[i]);
}
VB.NET Code

Dim _dt As New DataTable()
_dt = _ds.Tables(0)
Dim dt1 As DataTable = ds1.Tables(0)
For i As Integer = 0 To dt1.Rows.Count - 1
_dt.ImportRow(dt1.Rows(i))
Next

Comments