C#datatable operation

Source: Internet
Author: User

1"Add a DataTable DataSet.Tables.Add (DataTable) instance to the DataSet: DataSet DS=NewDataSet (); DataTable Table=NewDataTable ("Student Table"); Ds. Tables.add (table);2"Reading a DataTable DataTable from a DataSet=dataset.tables[0] or dataset.tables["Table name"] Example: DataTable table=ds[i] or DataTable table=ds["Student Table"]//i is the index value in the dataset because the dataset can hold multiple DataTable, you can find a DataTable based on the index value, or write the name of the DataTable directly to find a DataTable3the Add row DataTable t=NewDataTable (); DataRow R=T.newrow (); r["Column Name"]=column values; T.rows.add (R); Example: DataTable: Student table ID name1Xun code: DataTable T=NewDataTable ("Student Table"); DataRow R=T.newrow (); r["ID"]=2; r["name"]=xun2; T.rows.add (r);4the Add column DataTable.Columns.Add ("Column Name", Type.GetType ("Data Type"))  ;5"read column values from rows datarow["Column Name"] or datarow[datacolumn]; 6"read the column values from the DataTable DataTable table; A, table. rows[i]["Column Name"] B, table. Rows[i][i] C, table[i]. Column names (column names are not quoted)7"read out a particular row of DataTable table; Datarow[] SelectRow=table. Select ("Column name = '"+ Store a specific variable. ToString () +"'"); Select one of the rows: selectrow[index]
I. Datasets, DataTable, DataRow, DataColumn
Delete rows in a DataTable to be aware of indexing problems, there are generally two methods: 1 when using for loops, note that the counter initial value is the table length, which is the self-reducing loop. DataTable.Rows.RemoveAt (i) should be noted.  2"with the Select method of the DataTable, note that the parameter of the method is string filter 3" Delete () after a DataTable is required. The Acceptechanges () method confirms a complete deletion, because delete () only flags the status of the corresponding column as deleted, and it can be passed through a DataTable. RejectChanges () rollback to make the row undelete. To delete multiple rows, you can use Delete () consecutively, and then use the Acceptechanges () method to confirm the deletion.
ii. Delete rows in a DataTable three methods: (DataTable.Rows.Remove (DataRow Dr), DataTable.Rows.RemoveAt (i), Datarow.delete ())
DataTable dt = table. Defaultview.totable ();//File DataDataTable Sorcedt = Sorcedt = dateds2.tables[0];//Database DataDataTable except = table. Clone ();//Difference Set//finding differential Set data                            foreach(DataRow Drinchdt. Rows) {datarow[] dtemp= Sorcedt.select ("donationbarcode= '"+ dr["Donationbarcode"] +"' and Starttime= '"+ dr["StartTime"] +"'"); if(Dtemp. Length = =0) {DataRow DR2=except.                                    NewRow (); DR2=Dr; Except. Rows.Add (DR2.                                ItemArray); }                            }
Iii. Finding the difference set of two DataTable

C#datatable operation

Related Article

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.