2007-11-08

Copying Data from one DataTable to Another

You can use ImportRow method to do this by calling NewRow adds a row to the table using the existing table schema, but with default values for the row, and sets the DataRowState to Added. Calling ImportRow preserves the existing DataRowState along with other values in the row. If the DataRow that is passed as a parameter is in a detached state, it is ignored, and no exception is thrown.

Sample Code

For Each dr As DataRow In sourceTable.Rows

destinationTable.ImportRow(dr)

Next

But if the destination table have the same structure you can use this clone method copies the structure of the DataSet that including all datatable schemas, relations, and constraints.

Sample Code

Dim dsDestination As DataSet
' clone method copies the structure of the DataSet,
' including all datatable schemas, relations, and constraints
dsDestination = ds.Clone()

For Each dr As DataRow In ds.Tables(0).Rows
dsDestination.Tables(0).ImportRow(dr)
Next

vb.net sample code