DataTable usage tips

Source: Internet
Author: User

Protected void Page_Load (object sender, EventArgs e)
{
// ① Create a DataTable
DataTable dt = new DataTable ("Table_AX ");

// ② 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 );

// ③ 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 );

// ④ 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 '");

// ⑤ Copy DataTable include data
DataTable dtNew = dt. Copy ();

// ⑥ Copy DataTable only scheme
DataTable dtOnlyScheme = dt. Clone ();

// 7operate one row
//★All 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;

// Perform Evaluate another DataTable's row to current Datatable
DtOnlyScheme. Rows. Add (dt. Rows [0]. ItemArray );

// Use Rowstate
// ■
// Do not know how to change the Row State to DataRowState. Deleted
// More further, how to set the Row State
// ■
If (dt. Rows [0]. RowState = DataRowState. Unchanged)
{
// Your logic
}

// 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 ();

// I. string convert to DataTable [Doesn't achieve it]
// ■
// The string is not successfully converted to the DataTable ■ implemented, see Append ■
// ■
// DataTable dtConvert = new DataTable ();
// System. IO. StringReader stream = new System. IO. StringReader (s );
// System. Xml. XmlReader xtr = new System. Xml. XmlTextReader (stream );
// DtConvert. ReadXml (xtr );

// II. Filter DataTable
// It's so strange that the second row has been filtered
// The second row show in GridView never
// It means null field will be filter always.
// Filter the all conditions
Dt. DefaultView. RowFilter = "column1 <> true ";
// Dt. DefaultView. RowFilter = "column1 = true ";

Dt. DefaultView. RowStateFilter = DataViewRowState. Added;

// III. Sort row
// Stupid method
DataRow [] drsss = dt. Select (String. Empty, "column0 DESC, column1 ASC ");
// Clever method
Dt. DefaultView. Sort = "column0, column1 ASC ";

// IV. Bind DataTable
// Bind the defaview View
GvTestDataTable. DataSource = dt;
GvTestDataTable. DataBind ();
}
[Append] determines whether a string is the column name of the DataTable.
DtInfo. Columns. Contains ("AX ");
[Append] DataTable and XML Conversion
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;
} [Append] A Good sorting method
Dt. DefaultView. Sort = "ID, Name ASC ";
Dt = dt. DefaultView. ToTable ();

This article from the CSDN blog, reproduced please indicate the source: http://blog.csdn.net/jbgh608/archive/2007/10/29/1854686.aspx

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.