Inserting DataRow from a Datatable into Another Datatable

Posted by Anu_dgr8 under C# on 6/29/2010 | Views : 11762 | Status : [Member] | Replies : 4
dt2.Rows.InsertAt(dt1.Rows[0], Count-1);
I have 2 datatables dt1 and dt2..now i want to insert a datarow from dt1 into another datatable dt2..for this am doin as shown above..but its showing error as: THIS ROW ALREADY BELONGS TO ANOTHER TABLE.

I AM UNABLE TO FIGURE OUT WAT AM I DOIN WRONG...CAN SOME ONE TELL ME?

Thanks and Regards,
Hoosha Boosha



Responses

Posted by: NauhIL on: 6/30/2010 [Member] Starter

Up
0
Down
You need creat new row to dt2 like

DataRow dr = dt2.NewRow();

for (int i = 0; i < dt1.Columns.Count; i++)
{
dr[i] =dt1.Rows[0][i].ToString(); //insert dt1's row[0] to dt2
}
dt2.Rows.InsertAt(dr, 2);


Anu_dgr8, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Narayanjha on: 7/6/2010 [Member] Starter

Up
0
Down
Hi Anu,

You can try below code.

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

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


Regards,
Narayan

Anu_dgr8, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Priyagoel_24 on: 8/14/2013 [Member] Starter | Points: 25

Up
0
Down
dt1.Rows.InsertAt(dt2.Rows[0], Count-1);
I have 2 datatables dt1 and dt2..now i want to insert a datarow from dt1 into another datatable dt2..for this am doin as shown above..but its showing error as: THIS ROW ALREADY BELONGS TO ANOTHER TABLE.

Anu_dgr8, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Priyagoel_24 on: 8/16/2013 [Member] Starter | Points: 25

Up
0
Down
dt1.Rows.InsertAt
I have one datatables dt1 now i want to insert a datarow from dt1 into .for this am doin as shown above..but its showing error as: THIS ROW ALREADY BELONGS TABLE.
dt2.rows. i want check query how to make other dt2.
string check = "select name,days,hdays,months,years from inventory";
/ fill dt2.
if (dt1.Rows.Count > 0)
{ }
else
how to write the code.

Anu_dgr8, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response