Summary of c datatable usage

  • 2020-05-10 18:01:07
  • OfStack

1. DataTable profile
(1) constructor
DataTable() initializes a new instance of the DataTable class without arguments.
DataTable(string tableName) initializes a new instance of the DataTable class with the specified table name.
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 the subrelationships of this DataTable.
Columns gets the collection of columns that belong to the table.
Constraints gets the collection of constraints maintained by this table.
DataSet gets the DataSet to which this table belongs. Related information of DataSet can be found in 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 indicating whether there is an error in any row of any table of DataSet to which the table belongs.
MinimumCapacity gets or sets the initial start size of the table. The initial starting size of a row in this table. The default value is 50.
Rows gets the collection of rows that belong to the table.
TableName gets or sets the name of DataTable.
(3) common methods
AcceptChanges() commits all changes to the table since AcceptChanges() was last called.
BeginInit() starts initializing DataTable that is used on the form or by another component. Initialization occurs at run time.
Clear() clears DataTable of all data.
Clone() clones the structure of DataTable, including all the DataTable schemas and constraints.
EndInit() ends the initialization of DataTable used on a form or by another component. Initialization occurs at run time.
ImportRow(DataRow row) copies DataRow into DataTable, keeping any property Settings as well as the initial and current values.
Merge(DataTable table) merges the specified DataTable with the current DataTable.
NewRow() creates a new DataRow with the same schema as this table.

2. DataTable use tips
(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 select is null select is null 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
// is actually bound to DefaultView
gvTestDataTable.DataSource = dt;
gvTestDataTable.DataBind();
(13) judge the DataTable's Column name is a string
// determines whether a string is the column name of DataTable
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 articles: