MergeThe method is to combine two similar datatables into a datatable:
// Create table 1 datatable dt1 = new datatable (); // Add dt1.columns data. add ("ID"); dt1.columns. add ("name"); dt1.columns. add ("PWD"); For (INT I = 0; I <5; I ++) {dt1.rows. add (new object [] {I. tostring (), "name" + I. tostring (), "**"});} // create table 2 datatable dt2 = new datatable (); // Add dt2.columns data. add ("ID"); dt2.columns. add ("name"); dt2.columns. add ("PWD"); For (INT I = 5; I <10; I ++) {dt1.rows. add (new object [] {I. tostring (), "Name" + I. tostring (), "**"});} // create table 3 datatable dt3 = NULL; // copy the structure in table 1 to table 3dt3 = dt1.clone (); // MERGE table 1 and table 2 to Table 3if (dt3! = NULL) {dt3.merge (dt1); dt3.merge (dt2); this. datagridview1.datasource = dt3 ;}
Add datatable to Dataset
// Create ableabledatatable dt = new datatable (); DT. columns. add ("ID"); DT. columns. add ("name"); DT. columns. add ("PWD"); For (INT I = 0; I <10; I ++) {DT. rows. add (new object [] {I. tostring (), "name" + I. tostring (), "**"});} // Add the able to datasetdataset DS = new dataset (); DS. tables. add (DT); this. datagridview1.datasource = Ds. tables [0]. defaultview;