Summary of various methods for ASP. NET datatable operations (adding columns to datatable and selecting datatable for sorting)

Source: Internet
Author: User
1. create ableabledatatable dt = new datable (); // you can create a name for the table. tb2. 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 ");/ /Returns an array. insert the filtered content into another table. able 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 by DT. defaultview. sort = "id DESC"; 7. delete data DT. rows. remove (ROW); // Delete DT Based on Row information. rows. removeat (INDEX); // Delete 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.