Datatable methods (adding columns to datatable and selecting datatable for sorting, etc.

Source: Internet
Author: User

Extracted online, verified:

Create a able, add column names to the table datatable, Add rows to the table datatable, and filter the contents of the table datatable.
1. Create a able
Datatable dt = new datable (); // you can create a name for the table, TB
2. Add a column name to the table:
DT. Columns. Add ("ID", typeof (system. int32); // The type can be changed, such as system. int32, system. Double ..
DT. Columns. Add ("type", typeof (system. String ));
3. Add rows to the table. content:
Datarow ROW = DT. newrow ();
Row ["ID"] = 1;
Row ["type"] = "123 ";
DT. Rows. Add (ROW); // you can add
Row = DT. newrow ();
Row ["ID"] = 2;
Row ["type"] = "456 ";
DT. Rows. Add (ROW );
4. filter the table content and find the information with ID 1.
Datarow [] arr = DT. Select ("ID = 1"); // an array is returned.
5. Insert the filtered content into another table
Datatable dtnew = DT. Clone (); // copy the column information in the table DT to dtnew, instead of copying data.
Foreach (datarow row in ARR)
{
Dtnew. Rows. Add (ROW); // Add the filtered information to dtnew.
}
DT. acceptchanges (); // refresh the page after adding it!

6. Sort the table
DT. defaultview. Sort = "id DESC ";
7. delete data
DT. Rows. Remove (ROW); // delete a row based on the row information.
DT. Rows. removeat (INDEX); // delete an index
8. Modify DT column information
Row2 [0]. beginedit ();
Row2 [0] ["status"] = 0;
Row2 [0]. endedit ();

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.