C # detailed description of DataTable,

Source: Internet
Author: User

C # detailed description of DataTable,

Add reference

using System.Data;

Create a table

// Create an empty table DataTable dt = new DataTable (); // create an empty table DataTable dt = new DataTable ("Table_New") named "Table_New ");

Create a column

// 1. create an empty column DataColumn dc = new DataColumn (); dt. columns. add (dc); // 2. create a column with the column name and type name (either) dt. columns. add ("column0", System. type. getType ("System. string "); dt. columns. add ("column0", typeof (String); dt. columns. add (dc); // 3. add columns DataColumn dc = new DataColumn ("column1", System. type. getType ("System. dateTime "); DataColumn dc = new DataColumn (" column1 ", typeof (DateTime); dt. columns. add (dc );

Create row

// 1. create an empty row DataRow dr = dt. newRow (); dt. rows. add (dr); // 2. create an empty line dt. rows. add (); // 3. create and assign values to dt through the line framework. rows. add ("Zhang San", DateTime. now); // The data sequence of the parameters in Add must correspond to the sequence of the columns in dt.
// 4. Create a dt2 table by copying a row
Dt. Rows. Add (dt2.Rows [I]. ItemArray );

Value assignment and Value

// The value of DataRow dr = dt for the new row. newRow (); dr [0] = "James"; // assign dr ["column1"] = DateTime through the index. now; // assign values by name // assign values to existing rows of the table dt. rows [0] [0] = "Zhang San"; // assign dt values through the index. rows [0] ["column1"] = DateTime. now; // assign values by name // value: string name = dt. rows [0] [0]. toString (); string time = dt. rows [0] ["column1"]. toString ();

Filter rows

// Select DataRow [] drs = dt. select ("column1 is null"); // Select DataRow [] drs = dt for the set of rows whose column0 column value is "Li Si. select ("column0 = 'lily'"); // filter the DataRow [] drs = dt for the set of rows with "sheets" in column0 values. select ("column0 like 'sheet % '"); // if multiple criteria are selected, you can add and or // filter the set of rows with "sheets" in column0 values and sort DataRow [] drs = dt in descending order of column1. select ("column0 like 'sheet % '", "column1 DESC ");

Delete row

// Use DataTable. rows. remove (DataRow) method dt. rows. remove (dt. rows [0]); // use DataTable. rows. removeAt (index) method dt. rows. removeAt (0); // use DataRow. delete () method dt. row [0]. delete (); dt. acceptChanges (); // differences and notes ----- // The Remove () and RemoveAt () methods directly Delete // Delete () methods only mark this row as deleted, however, it still exists and can be a DataTable. the RejectChanges () rollback cancels the deletion of this row. // When you use Rows. Count to retrieve the number of Rows, you must use the able. AcceptChanges () method to submit the changes. // If you want to delete multiple rows in the DataTable, you should use the inverted loop DataTable. rows, and you cannot use foreach to perform cyclic deletion, because the index changes during the positive order deletion, and the program has an exception, which is difficult to predict. For (int I = dt. Rows. Count-1; I> = 0; I --) {dt. Rows. RemoveAt (I );}

Copy a table

// Copy the table and the data DataTable dtNew = new DataTable (); dtNew = dt. copy (); // Copy the table DataTable dtNew = dt. copy (); // Copy the data structure of the dt table dtNew. clear () // Clear data for (int I = 0; I <dt. rows. count; I ++) {if (Condition Statement) {dtNew. rows. add (dt. rows [I]. itemArray); // Add a data row} // clone the table, only copying the table structure, excluding the data DataTable dtNew = new DataTable (); dtNew = dt. clone (); // If you only need a row of able dtNew = new DataTable (); dtNew = dt. copy (); dtNew. rows. clear (); // Clear table data dtNew. importRow (dt. rows [0]); // This is the first line added

Table sorting

DataTable dt = new DataTable (); // create a table dt. columns. add ("ID", typeof (Int32); // Add the column dt. columns. add ("Name", typeof (String); dt. columns. add ("Age", typeof (Int32); dt. rows. add (new object [] {1, "Zhang San", 20}); // Add a row dt. rows. add (new object [] {2, "Li Si", 25}); dt. rows. add (new object [] {3, "Wang Wu", 30}); DataView dv = dt. defaultView; // obtain the table view dv. sort = "id desc"; // Sort dv by ID in reverse order. toTable (); // convert to table

 

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.