How can I copy specific rows from DataTable to another Datable in c#? There will be more than one row.
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().
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.
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); } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With