Two tables A and B have the same structure. Now we need to copy the branch in the middle of Table A to table B.
If datatableb. Rows. Add (datatablea. Rows [0]) is directly used, an error such as "row already belongs to table a" is reported.
So we can use another method.
Method 1: Use the datatable. importrow () method. The Code is as follows: // obtain some rows in table.
Datarow [] DRA = DTA. Select ("aimtype = 3 ");
// Instance B
Datatable dtb = DTA. Clone ();
If (DRA. length> 0)
{
Foreach (datarow drval in DRA)
{
// Add rows to B
Dtb. importrow (drval );
}
}
Method 2: Use the datatable. Rows. Add (Params object [] values) method. The Code is as follows: // obtain some rows in table.
Datarow [] DRA = DTA. Select ("aimtype = 3 ");
// Instance B
Datatable dtb = DTA. Clone ();
If (DRA. length> 0)
{
Foreach (datarow drval in DRA)
{
// Add rows to B
Dtb. Rows. Add (drval. itemarray );
}
} Chen Kuan Tian has two tables A and B with the same structure. Now we need to copy the branch in the middle of Table A to table B.
If datatableb. Rows. Add (datatablea. Rows [0]) is directly used, an error such as "row already belongs to table a" is reported.
So we can use another method.
Method 1: Use the datatable. importrow () method. The Code is as follows: // obtain some rows in table.
Datarow [] DRA = DTA. Select ("aimtype = 3 ");
// Instance B
Datatable dtb = DTA. Clone ();
If (DRA. length> 0)
{
Foreach (datarow drval in DRA)
{
// Add rows to B
Dtb. importrow (drval );
}
}
Method 2: Use the datatable. Rows. Add (Params object [] values) method. The Code is as follows: // obtain some rows in table.
Datarow [] DRA = DTA. Select ("aimtype = 3 ");
// Instance B
Datatable dtb = DTA. Clone ();
If (DRA. length> 0)
{
Foreach (datarow drval in DRA)
{
// Add rows to B
Dtb. Rows. Add (drval. itemarray );
}
}