Usage of DataTable

Source: Internet
Author: User

I. Introduction of the DataTable
(1) Constructors
A DataTable () Initializes a new instance of the DataTable class without parameters.
A DataTable (String tableName) Initializes a new instance of the DataTable class with the specified table name.
The DataTable (String TableName, String tablenamespace) Initializes a new instance of the DataTable class with the specified table name and namespace.
(2) Common Properties
CaseSensitive indicates whether string comparisons in the table are case-sensitive.
ChildRelations gets the collection of child relationships for this DataTable.
Columns gets the collection of columns that belong to the table.
Constraints gets the collection of constraints maintained by the table.
The dataset gets the dataset to which this table belongs. DataSet related information, see my previous article "Data Access (2)-dataset"
DefaultView gets a custom view of a table that might include a filtered view or cursor position.
HasErrors gets a value that indicates whether there is an error in any row of any table of the dataset to which the table belongs.
Minimumcapacity Gets or sets the initial starting size of the table. The initial starting size of the rows in the table. The default value is 50.
Rows gets the collection of rows that belong to the table.
TableName Gets or sets the name of the DataTable.
(3) Common methods
AcceptChanges () submits all changes made to the table since the last call to AcceptChanges ().
BeginInit () begins initializing a DataTable that is used on a form or used by another component. Initialization occurs at run time.
Clear () Clears the DataTable for all data.
Clone () Clones the structure of a DataTable, including all DataTable schemas and constraints.
EndInit () ends the initialization of a DataTable that is used on a form or used by another component. Initialization occurs at run time.
ImportRow (DataRow Row) copies the DataRow into a DataTable, preserving any property settings as well as the initial and current values.
The merge (DataTable table) merges the specified DataTable with the current DataTable.
NewRow () Creates a new DataRow with the same schema as the table.

Second, the use of the DataTable skills
(1) Create a DataTable
DataTable dt = new DataTable ("Table_ax");
(2) ADD columns for DataTable
Method 1
Dt. Columns.Add ("Column0", System.Type.GetType ("System.String"));
Method 2
DataColumn dc = new DataColumn ("Column1", System.Type.GetType ("System.Boolean"));
Dt. Columns.Add (DC);
(3) Add rows for DataTable
Initialize the row
DataRow dr = dt. NewRow ();
dr["column0"] = "AX";
dr["Column1"] = true;
Dt. Rows.Add (DR);
Doesn ' t initialize the row
DataRow DR1 = dt. NewRow ();
Dt. Rows.Add (DR1);
(4) Select row
Search the second row if there is no assignment, select with IS null
datarow[] DRS = dt. Select ("Column1 is null");
datarow[] Drss = dt. Select ("Column0 = ' AX '");
(5) Copy DataTable include data
DataTable dtnew = dt. Copy ();
(6) Copy DataTable only Scheme
DataTable dtonlyscheme = dt. Clone ();
(7) Operate one row
Operation on DT
Method 1
DataRow droperate = dt. Rows[0];
droperate["column0"] = "axzhz";
droperate["Column1"] = false;
Method 2
Droperate[0] = "Axzhz";
DROPERATE[1] = false;
Method 3
Dt. rows[0]["column0"] = "axzhz";
Dt. rows[0]["Column1"] = false;
Method 4
Dt. Rows[0][0] = "Axzhz";
Dt. ROWS[0][1] = false;
(8) Evaluate Another DataTable ' s row to current DataTable
DTONLYSCHEME.ROWS.ADD (dt. Rows[0]. ItemArray);
(9) Convert to String
System.IO.StringWriter SW = new System.IO.StringWriter ();
System.Xml.XmlTextWriter XW = new System.Xml.XmlTextWriter (SW);
Dt. WRITEXML (XW);
string s = sw. ToString ();
(Ten) Filter DataTable
Dt. Defaultview.rowfilter = "Column1 <> true";
Dt. Defaultview.rowfilter = "Column1 = true";
(one) Sort row
Dt. Defaultview.sort = "ID, Name ASC";
Dt=dt. Defaultview.totable ();
() Bind DataTable
The binding is actually defaultview.
Gvtestdatatable.datasource = DT;
Gvtestdatatable.databind ();
Judge the DataTable ' s Column name is a string
Determines whether a string is a column name for a DataTable
DtInfo.Columns.Contains ("AX");

Usage of 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.