C # Able's implicit usage

Source: Internet
Author: User

Datatable is often used in projects. If datatable is used properly, it not only makes the program concise and practical, but also improves performance and achieves twice the result with half the effort. The usage skills of datatable are summarized.

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

(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
// The binding is actually defaview view gvtestable able. datasource = DT; gvtestdatatable. databind ();

(13) judge the datatable's column name is a string
// Determine whether a string is the datatable column name dtinfo. Columns. Contains ("ax ");

(14) 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() 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;        }
Related Article

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.