copy a single row from one datatable to other

MaxRecursion picture MaxRecursion · Apr 26, 2012 · Viewed 54.1k times · Source

I have two datatables one has few rows other is empty. I am running a loop over first one to copy some of the rows to another table. I am getting error 'The row already belongs to another table'.

Is there any way to copy DataRows one by one to other DataTable.

thanks in advance

Answer

Nikhil Agrawal picture Nikhil Agrawal · Apr 26, 2012

Use

newtable.ImportRow(oldtable.Rows[i]) 

where i is the desired row number.

as explained in http://support.microsoft.com/kb/308909/en-us