A Method for copying datatable

Source: Internet
Author: User
If Program The returned datatable is added to a custom dataset. The ds. Tables. Add (DT) method is used directly, and the error "This datatable already belongs to another dataset" is displayed. At this time, we need to create a new able and import the structure and data of the original datatable to the new datatable. At this time, we can use the following method (of course, the method is not the only one):

/**/ ///   <Summary>
/// Copy datatable
///   </Summary>
///   <Param name = "copydt"> Data Source </Param>
///   <Param name = "tablename"> New able name </Param>
///   <Returns> Replicated able </Returns>
Private Datatable copydatatable (datatable copydt, String Tablename)
{
Datatable dt =   New Datatable (tablename );
Foreach (Datacolumn columm In Copydt. columns)
{
DT. Columns. Add (columm. columnname, columm. datatype );
}

Foreach (Datarow row In Copydt. Rows)
{
DT. begininit ();
DT. importrow (ROW );
DT. endinit ();
}

Return DT;
}

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.