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. 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");
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.TextWriter 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;
}
This article originates from the network reprint