In the project is often used in the DataTable, if the DataTable used properly, not only can make the program concise and practical, but also can improve performance, to achieve a multiplier effect, now the use of the DataTable to summarize the skills.
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.
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 () clear 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) the DataRow Copy into the DataTable, preserving any property settings as well as the initial and current values.
Merge (DataTable table) will specify the DataTable Merge with the current DataTable.
NewRow () Create a new DataRow with the same schema as the table.
Second, DataTable usage tips
(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"));
(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 no value is assigned, select datarow[] DRS = DT with IS null. 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 of the 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 DataTable column name dtInfo.Columns.Contains ("AX");
DataTable Convert to XML and XML convert to DataTable
protected void Page_Load (object sender, EventArgs e) {DataTable dt_ax = new DataTable (); Dt_ax. Columns.Add ("Sex", typeof (System.Boolean)); DataRow dr = Dt_ax. NewRow (); dr["Sex"] = true; Dt_ax. Rows.Add (DR); String Xml=convertbetweendatatableandxml_ax (Dt_ax); DataTable dt = Convertbetweendatatableandxml_ax (XML); } public string Convertbetweendatatableandxml_ax (DataTable dtneedcoveret) {System.IO.TextWrite R tw = new System.IO.StringWriter (); If TableName is empty, WriteXml () would throw Exception. Dtneedcoveret.tablename=dtneedcoveret.tablename.length==0? " Table_ax ":d tneedcoveret.tablename; Dtneedcoveret.writexml (TW); Dtneedcoveret.writexmlschema (TW); return TW. ToString (); } Public DataTable Convertbetweendatatableandxml_ax (string xml) {System.IO.TextReadeR trdatatable = new System.IO.StringReader (XML. Substring (0, XML. IndexOf ("<?xml")); System.IO.TextReader Trschema = new System.IO.StringReader (XML. Substring (XML. IndexOf ("<?xml")); DataTable Dtreturn = new DataTable (); Dtreturn.readxmlschema (Trschema); Dtreturn.readxml (trdatatable); return dtreturn; }
(15) Executing a query in a DataTable returns a DataTable;
Method 1:
//<summary>//executing a query in a DataTable returns a new DataTable//</summary>//DT is the source data DataTable//condition is a query conditionDataTable NEWDT=NewDataTable (); NEWDT= dt. Clone ();//cloning DT structure, including all DT architectures and constraints, and no data;datarow[] rows = dt. Select (conditions);//search for qualified records from DT;foreach(DataRow rowinchRows//Add the results of the query to the DT;{NEWDT. Rows.Add (row. ItemArray);}
Method 2:
New DataTable (); NEWDT== dt. Select (condition); for (int i=0; i<dr. length;i++) {Newdt. ImportRow ((DataRow) dr[i]);}
Method 3:
New"conditions"=dt. Defaultview.totable ();
(16) Delete rows from a DataTable:
Delete Rows in a DataTable three methods: (DataTable.Rows.Remove (DataRow Dr), DataTable.Rows.RemoveAt (i), Datarow.delete ())
Delete Rows in a DataTable to be aware of indexing problems, there are generally two methods:
1 "With A For loop, note that the counter initial value is the table length, the self-decrement loop. DataTable.Rows.RemoveAt (i) should be noted.
2 "With the Select method of the DataTable, note that the parameter of the method is a string filter
A DataTable is required after 3 "Delete (). 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.
Common usage of C # DataTable explained