Datatable's merge method and addition of datatable to Dataset

Source: Internet
Author: User
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; 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.