We often need to add a row of data to the datatable. In most cases, we add some data collected from the UI controls and program variables to the datatable. Add the Supplier Code and name to the able as follows:
Datatable dtprovider = new datatable ();
Datarow drrow = dtprovider. newrow ();
Drrow [0] = txtprovidercode. Text. Trim ();
Drrow [1] = txtprovidername. Text. Trim ();
Dtprovider. Rows. Add (drrow );
In most cases, these statements fully meet the requirements. However, if you want to add a row of another datatable with the same structure to this dtprovider, you cannot simply add it. Otherwise, an error "this row belongs to another table." is displayed .". At this time, we must define another datarow, assign the data of the source datarow to the destination datarow, and then add the data to the able. As follows:
Datatable dtprovider = new datatable ();
Datarow drtarget = dtprovider. newrow ();
Drtarget. itemarry = drsource. itemarry;
// Note: The drsource is a row in another datatable with the same structure.
Dtprovider. Rows. Add (drtarget );