C # datatable usage Summary

Source: Internet
Author: User
Tags throw exception

1. DataTable Introduction
(1) constructor
DataTable () initializes a new instance of the DataTable class without parameters.
DataTable (string tableName) uses the specified table name to initialize a new instance of the DataTable class.
DataTable (string tableName, string tableNamespace) uses the specified table name and namespace to initialize a new instance of the DataTable class.
(2) common attributes
CaseSensitive indicates whether the string comparison in the table is case sensitive.
ChildRelations obtains the set of child relations of this able.
Columns obtains the set of Columns that belong to the table.
Constraints obtains the set of Constraints maintained by the table.
DataSet obtains the DataSet to which the table belongs. DataSet-related information. See my previous article Data Access (2)-DataSet.
DefaultView: obtains the custom view of a table that may contain a filtering view or a cursor position.
HasErrors gets a value that indicates whether there is an error in any row of any table in the DataSet to which the table belongs.
MinimumCapacity gets or sets the initial start size of the table. The initial starting size of the row in the table. The default value is 50.
Rows obtains the set of Rows belonging to the table.
TableName: Get or set the name of the DataTable.
(3) Common Methods
AcceptChanges () submits all changes made to the table since the last call of AcceptChanges.
BeginInit () starts to initialize the able that is used on the form or used by another component. Initialization occurs at runtime.
Clear () clears the DataTable of all data.
Clone (): Clone the structure of the able, including all the DataTable architectures and constraints.
EndInit () ends the initialization of the able used on the form or used by another component. Initialization occurs at runtime.
ImportRow (DataRow row) Copies DataRow to the DataTable, retaining any attribute settings, initial values, and current values.
Merge (DataTable table) combines the specified able with the current DataTable.
NewRow () creates a new DataRow with the same schema as the table.

2. DataTable usage 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 no value is assigned, use is null to select
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
// Actions 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 ();
(10) Filter DataTable
Dt. DefaultView. RowFilter = "column1 <> true ";
Dt. DefaultView. RowFilter = "column1 = true ";
(11) Sort row
Dt. DefaultView. Sort = "ID, Name ASC ";
Dt = dt. DefaultView. ToTable ();
(12) Bind DataTable
// Bind the defaview View
GvTestDataTable. DataSource = dt;
GvTestDataTable. DataBind ();
(13) judge the DataTable's Column name is a string
// Determine whether a string is the name of the DataTable Column
DtInfo. Columns. Contains ("AX ");
(14) DataTable convert to XML and XML convert to DataTableCopy 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 () will throw Exception.
DtNeedCoveret. TableName = dtNeedCoveret. TableName. Length = 0? "Table_AX": dtNeedCoveret. 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.