C # DataTable Operations Summary Dry Goods

Source: Internet
Author: User

The first one is to add a DataTable and operate the second is to operate on the existing table basically all should have covered if there is no message I am good to update.

One

Add Reference

Using System.Data;

Create a table

Create an empty table datatable dt = new DataTable ();//Create an empty table named "Table_new" datatable dt = new DataTable ("Table_new");

Create column existing column only row

1. Create an empty column DataColumn dc = new DataColumn ();d T. Columns.Add (DC);//2. Create columns with column names and type names (two ways to choose one) This method adds the  DT directly. Columns.Add ("Column0", System.Type.GetType ("System.String"));d T. Columns.Add ("Column0", typeof (String)),//3. Adding columns through the column schema exists in the object and then adds columns DataColumn DC = new DataColumn ("Column1", System.Type.GetType ("System.DateTime"));D atacolumn dc = new DataColumn ("Column1", typeof (DateTime));d T. Columns.Add (DC);

Create rows

1. Create the empty line DataRow dr = dt. NewRow ();d T. Rows.Add (DR);//2. Create a blank line dt. Rows.Add ();//3. Create and assign the DT through the row frame. Rows.Add ("Zhang San", DateTime.Now); The data order of the parameters in the//add corresponds to the order of the columns in the DT
One by one correspondence is wrong, it's not good.

4. Create a row of the DT2 table by copying it, but only if DT2 and DT are the same in each column
Dt. Rows.Add (DT2. Rows[i]. ItemArray);

assigning and taking values

The assignment of the new row is the DataRow dr = dt. NewRow ();d r[0] = "Zhang San";//value is assigned by index dr["Column1"] = DateTime.Now; Assigns a value by name//to the table already has the row to assign the value dt. Rows[0][0] = "Zhang San"; The index is assigned by DT. rows[0]["Column1"] = datetime.now;//is assigned by name//value string Name=dt. Rows[0][0]. ToString (); string Time=dt. rows[0]["Column1"]. ToString ();

Filter rows

Select the Column1 column value is empty for the collection of rows datarow[] DRS = dt. Select ("Column1 is null");//Choose the Column0 column value as "John Doe" for the collection of rows datarow[] DRS = dt. Select ("Column0 = ' john Doe '");//Filter the collection of rows with "sheets" in the Column0 column value (fuzzy query) datarow[] DRS = dt. Select ("Column0 like '% '");//If you have a multi-condition filter, you can add and or or//to filter the collection of rows that have "sheets" in the Column0 column values and sort by column1 in descending order datarow[] drs = dt. Select ("Column0 like ' Zhang% '", "Column1 DESC");

Delete Row

Use the DataTable.Rows.Remove (DataRow) method dt. Rows.remove (dt. Rows[0]);//Use the DataTable.Rows.RemoveAt (index) method dt. Rows.removeat (0);//Use the Datarow.delete () method dt. Row[0]. Delete ();d T. AcceptChanges ();//-----Differences and points of note-----//remove () and RemoveAt () method is to directly delete the//delete () method just to mark the row as deleted, but there are You can also datatable.rejectchanges () rollback to make the row undelete. When you use Rows.Count to get the number of rows, or the number of rows before you delete, you need to use the Datatable.acceptchanges () method to commit the modifications. If you want to delete multiple rows in a DataTable, you should use the reverse loop datatable.rows, and you cannot use foreach to iterate, because the index changes when the positive sequence is deleted, the program is abnormal, and it is difficult to anticipate the consequences. for (int i = dt. rows.count-1; I >= 0; i--) {dt. Rows.removeat (i);}

Copying tables

Copy table, copy table structure and table data datatable dtnew = new DataTable ();d tnew = dt. Copy ();//Duplicate table datatable dtnew = dt. Copy ();  Copy DT table data structure dtnew.clear ()  //Empty data for (int i = 0; i < dt. Rows.Count; i++) {    if (conditional statement)    {         dtNew.Rows.Add (dt. Rows[i]. ItemArray);  Add data row    }}//clone table, just copy the table structure, not including data datatable dtnew = new DataTable ();d tnew = dt. Clone ();//If only one row in a table is required DataTable dtnew = new DataTable ();d tnew = dt. Copy ();d tNew.Rows.Clear ();//Clear table Data dtnew.importrow (dt. Rows[0]);//This is the first line to join.

Table sort

DataTable dt = new DataTable ();//CREATE TABLE dt. Columns.Add ("ID", typeof (Int32));//Add Column dt. Columns.Add ("Name", typeof (String));d T. Columns.Add ("Age", typeof (Int32));d T. Rows.Add (New object[] {1, "Zhang San", 20});//Add Line dt. Rows.Add (New object[] {2, "John Doe",);d T. Rows.Add (New object[] {3, "Harry", ());D Ataview dv = dt. defaultview;//Gets the table view DV. sort = "id DESC";//Sort dv.totable () in reverse order of ID;//Convert to Table



C # DataTable Operations Summary Dry Goods

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.