Move DataRow to other DataTable

D

Darren

I'm trying to move a row from one table to another. I get the error message that
the row already belongs to another table even though I have removed it from the table.
I'm trying to avoid recreating the row for performance reasons.

DataTable dataTable1 = dataTable2.Clone();

dataTable1.Rows.Remove(row);
dataTable2.Rows.Add(row);

TIA
 
R

RobinS

Try putting an EndEdit after the Rows.Remove(row). This will commit
the changes to the DataTable.

It's odd; it seems like you should add it to the second datatable
before removing it from the first.

Robin S.
 
D

diego

Ayon kay Darren:
I'm trying to move a row from one table to another. I get the error message that
the row already belongs to another table even though I have removed it from the table.
I'm trying to avoid recreating the row for performance reasons.

DataTable dataTable1 = dataTable2.Clone();

dataTable1.Rows.Remove(row);
dataTable2.Rows.Add(row);

TIA


try the ff.:

datatable2.ImportRow(row)
datatable1.Rows.Remove(row)

HTH
 
W

WenYuan Wang

Hi Darren,
According to your description, I understand that you would like to move a
row from one table to another table.
If I misunderstand anything here, please don't hesitate to correct me.
Thanks.

You can use the following code to move the row from DataTable1 to DataTable2

DataTable dataTable2 = dataTable1.Clone();
//copy rowdata from table1 to table2
dataTable2.Rows.Add(row.ItemArray);
//remove rowdata from table1
dataTable1.Rows.Remove(row);

If there is anything unclear, please feel free to reply me and we are glad
to work with you.
Have a great day.
Best regards,
Wen Yuan
 
W

WenYuan Wang

Hi Darren,

Just want to check if there is anything we can help with.
Please feel free to reply me here and we will follow up.

Happy New Year!
Sincerely,
Wen Yuan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top