How to add datarow of another able to datatable

Source: Internet
Author: User
Tags xsl

We often need to add a row of data to the datatable. In most cases, we add some data collected from the UI controls and program variables to the datatable. Add the Supplier Code and name to the able as follows:

Datatable dtprovider = new datatable ();
Datarow drrow = dtprovider. newrow ();
Drrow [0] = txtprovidercode. Text. Trim ();
Drrow [1] = txtprovidername. Text. Trim ();
Dtprovider. Rows. Add (drrow );

 

In most cases, these statements fully meet the requirements. However, if you want to add a row of another datatable with the same structure to this dtprovider, you cannot simply add it. Otherwise, an error "this row belongs to another table." is displayed .". At this time, we must define another datarow, assign the data of the source datarow to the destination datarow, and then add the data to the able. As follows:

Datatable dtprovider = new datatable ();
Datarow drtarget = dtprovider. newrow ();
Drtarget. itemarry = drsource. itemarry;
// Note: The drsource is a row in another datatable with the same structure.
Dtprovider. Rows. Add (drtarget );

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.