Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy rows from one Datatable to another DataTable?

Tags:

c#

datatable

How can I copy specific rows from DataTable to another Datable in c#? There will be more than one row.

like image 811
kartal Avatar asked Oct 26 '10 02:10

kartal


People also ask

How do I copy DataTable from one DataTable to another?

Copy() creates a new DataTable with the same structure and data as the original DataTable. To copy the structure to a new DataTable, but not the data, use Clone().


2 Answers

foreach (DataRow dr in dataTable1.Rows) {     if (/* some condition */)         dataTable2.Rows.Add(dr.ItemArray); } 

The above example assumes that dataTable1 and dataTable2 have the same number, type and order of columns.

like image 73
Bradley Smith Avatar answered Sep 18 '22 23:09

Bradley Smith


Copy Specified Rows from Table to another

// here dttablenew is a new Table  and dttableOld is table Which having the data   dttableNew  = dttableOld.Clone();    foreach (DataRow drtableOld in dttableOld.Rows) {    if (/*put some Condition */)    {       dtTableNew.ImportRow(drtableOld);    } } 
like image 39
Rageesh Geetha Raman Avatar answered Sep 18 '22 23:09

Rageesh Geetha Raman