The merge method combines two data tables with similar structures into one data table:
Instance Code :
// Create Table1 Datatable dt1 = New Datatable (); dt1.columns. 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 Table2 Datatable dt2 = New Datatable (); dt2.columns. 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 (), " ** " });} // Merge two tables Datatable dt3 = Null ; Dt3 = Dt1.clone (); If (Dt3 ! = Null ) {Dt3.merge (dt1); dt3.merge (dt2 ); This . Datagridview1.datasource = Dt3 ;}
Add datatable to Dataset
Datatable 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 (), " ** " });} Dataset DS = New Dataset (); DS. Tables. Add (DT ); This . Datagridview1.datasource = DS. Tables [ 0 ]. Defaultview;