C # Operations DataTable

Source: Internet
Author: User

1. Create a DataTable

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));//

Types can be transformed, such as

System.int32,system.double.

Dt. Columns.Add ("type", typeof (System.String));

3. Add lines to the table, content:

DataRow Row=dt. NewRow ();

row["id"]=1;

row["type"]= "123";

Dt. Rows.Add (row);//

This allows you to add a

Row=dt. NewRow ();

row["id"]=2;

row["type"]= "456";

Dt. Rows.Add (row);

4. Filter the contents of the table, and look for information with ID 1

DataRow []arr=dt. Select ("id=1");//

The return is an array

5. Insert the filtered content into another table

DataTable Dtnew=dt. Clone ();//

Copy the column information in the table dt dtnew, not copy the data.

foreach (DataRow row in arr)

{

Dtnew. Rows.Add (row);//

The filtered information is added to the

Dtnew

In

}

Dt. AcceptChanges ();//

After you add it, remember to refresh it.

!

6. Sorting tables

Dt. Defaultview.sort = "id desc";

7. Delete Data

Dt. Rows.remove (row);//

Under

Row

The line information is removed from DT. Rows.removeat (index);//

Delete based on index indexes

8. Modify the DT column information

Row2[0]. BeginEdit ();

row2[0]["status"] = 0;

Row2[0]. EndEdit ();

C # Operations DataTable

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.