How to reset a DataTable object in a DataSet

Source: Internet
Author: User
Tags reset
Object we can contain multiple DataTable objects in a dataset.

DataSet myDataSet = new DataSet ();
DataTable TableA = new DataTable ("A");
DataTable TableB = new DataTable ("B");

MYDATASET.TABLES.ADD (TableA);
MYDATASET.TABLES.ADD (TableB);

This includes a TableA and a TableB two DataTable objects in the myDataSet, and we access the DataTable contained in the DataSet through Dataset.tables properties. For example, you can access A TableA object by using this statement: mydataset.tables["A". But the datatablecollection indexer (in fact dataset.tables is datatablecollection type) can only get a DataTable object from the user and cannot be set.

mydataset.tables["A"] = mydataset.tables["B"]; Bad syntax

Such a statement can be compiled with an error. So how do we reset a TableA? Re-declare a DataTable object, and then copy TableB to the new DataTable object.

DataTable newtable;
newtable = mydataset.tables["B"]. Copy ();

The structure and data in the newtable are the same as the TableB in this case. But how do we give newtable copy to TableA? Try the following code

Newtable.tablename = "A";
MyDataSet.Tables.Remove ("A");
MYDATASET.TABLES.ADD (newtable):
Remove the original TableA from the dataset, set the newtable name to the TableA name, and add newtable to the dataset so we can get a new TableA





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.