How to merge two data tables with the same structure

Source: Internet
Author: User

Today, we encountered a situation where we read the information of several customers meeting condition 1 from a data table, read the information of several customers meeting condition 2, and finally display the information. because the structure of the customer information of the first and second data is identical, it is better to merge the data into a able and assign the value to the gridview. wrote a function.

/// <Summary>
/// Merge two identical datatables and return the Merged Results
/// </Summary>
/// <Param name = "dt1"> </param>
/// <Param name = "dt2"> </param>
/// <Returns> </returns>
Public datatable combinethesamedatatable (datatable dt1, datatable dt2)
{
If (dt1.rows. Count = 0 & dt2.rows. Count = 0)
{
Return new datatable ();
}
If (dt1.rows. Count = 0)
{
Return dt2;
}
If (dt2.rows. Count = 0)
{
Return dt1;
}
Dataset DS = new dataset ();
DS. Tables. Add (dt1.copy ());
DS. Merge (dt2.copy ());
Return Ds. Tables [0];
}

Due to the limited understanding of C #, I don't know why I cannot correctly judge the empty table with dt1 = NULL, so I used the method of several rows.

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.