Monday, March 12, 2007

DataTable.ImportRow Method to copy rows from a table to another table.

This small code shows how to use the DataTable.ImportRow Method to copy rows from a table to another table.

The ImportRow method of DataTable copies a row into a DataTable with all of the properties and data of the row. It actually calls NewRow method on destination DataTable with current table schema and sets DataRowState to Added.

DataTable dt; /// fill the table before you use it
DataTable copyto;

foreach(DataRow dr in dt.Rows)
{
copyto.ImportRow(dr);
}


the above code copies all the rows of the table dt to the table copyto

No comments: