C # DataTable Usage Summary

Source: Internet
Author: User
Tags add copy include string sort table name throw exception tostring
A brief introduction of the DataTable
(1) constructor
The DataTable () Initializes a new instance of the DataTable class with no parameters.
A DataTable (String tablename) Initializes a new instance of the DataTable class with the specified table name.
A 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 a table are case-sensitive.
ChildRelations gets a collection of child relationships for this DataTable.
Columns gets the collection of columns that belong to the table.
Constraints gets a collection of the constraints maintained by the table.
The dataset obtains 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 location.
HasErrors gets a value that indicates whether there are errors in any row in any of the tables 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 () commits any changes 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 the 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 DataRow to the DataTable, leaving any property settings and 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 DataTable techniques
(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 no value is assigned, 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
Operations 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 ();
(a) Bind DataTable
The binding is actually defaultview.
Gvtestdatatable.datasource = DT;
Gvtestdatatable.databind ();
(judge) The DataTable ' s Column name is a string
A column name that determines whether a string is a DataTable
DtInfo.Columns.Contains ("AX");
DataTable Convert to XML and XML convert to DataTable
Copy CodeThe code is as follows:
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 () to 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;
}


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.