What Is a Data Table?
DataTable
data sheet
- This entry lacks an overview map . Supplementing related content makes the entry more complete and can be upgraded quickly. Come on!
- Chinese name
- data sheet
- Foreign name
- DataTable
- Belong to
- Grid virtual table that holds data temporarily
- Category
- Core objects in the ADO dot net library
- Application
- VB and ASP
- Adding data to a data table
- Explains how to create new rows and add them to the table.
- View data in a data table
- Explains how to access the data in a row, including the original and current versions of the data.
- Load method
- Explains how to fill a DataTable with rows through the Load method.
- DataTable edit
- Explains how to modify the data in a row, including pending changes to the row, until the proposed changes are validated and accepted.
- Row status and row version
- Provides information about the different states of a row.
- DataRow delete
- Explains how to remove rows from a table.
- Line error message
- Explains how to insert error messages for each row to help resolve data issues in your application.
- AcceptChanges and RejectChanges
- Describes how to accept or reject changes to a row.
- DataTable
- It is a grid virtual table that temporarily stores data (a table representing data in memory.). DataTable is the core object in the ADO dot net library. It can be applied to VB and ASP. It can simply bind the database without code. It has a Microsoft-style user interface.
- Other objects that use DataTable include DataSet and DataView.
- DataTable is a grid virtual table (a table representing data in memory) that temporarily holds data. DataTable is the core object in the ADO dot net library. It can be applied to VB and ASP. It can simply bind the database without code. It has a Microsoft-style user interface.
- Other objects that use DataTable include DataSet and DataView.
- When accessing DataTable objects, please note that they are case sensitive by condition. For example, if one DataTable is named "mydatatable" and the other is named "Mydatatable", the string used to search one of the tables is considered case sensitive. However, if "mydatatable" exists and "Mydatatable" does not exist, the search string is considered case-insensitive. A DataSet can contain several DataTable objects, which have the same TableName property value and different Namespace property values. More information on using the DataTable object.
- Create DataTable
- DataTable represents a table of in-memory relational data, which can be created and used independently, or used by other .NET Framework objects. The most common case is as a member of a DataSet.
- A DataTable object can be created using the corresponding DataTable constructor. You can add it to the DataSet object's Tables collection by using the Add method and add it to the DataSet .
- You can also create a DataTable object by using the Fill method or FillSchema method of the DataAdapter object in the DataSet , or using the ReadXml , ReadXmlSchema, or InferXmlSchema methods of the DataSet from a predefined or inferred XML schema. Note that once a DataTable is added as a member to the Tables collection of a DataSet , it cannot be added to the table collection of any other DataSet .
- When you first create a DataTable , there is no schema (ie structure). To define the table's schema, you must create a DataColumn object and add it to the table's Columns collection. You can also define primary key columns for a table, and you can create Constraint objects and add them to the table's Constraints collection. After defining the schema for the DataTable , you can add data rows to the table by adding the DataRow object to the Rows collection of the table.
- When you create a DataTable , you don't need to provide a value for the TableName property, you can specify it at another time or leave it blank. However, when a table without a TableName value is added to the DataSet , the table will get a default name Table n that increments from "Table" (meaning Table0).
- DataTable schema definition
- The schema (that is, structure) of a table is represented by columns and constraints. Use a DataColumn object and ForeignKeyConstraint and UniqueConstraint objects to define the schema of the DataTable. Columns in a table can be mapped to columns in a data source, contain values calculated from expressions, auto-increment their values, or contain primary key values.
- Referencing columns, relationships, and constraints in a table by name is case sensitive. Therefore, two or more columns, relationships, or constraints with the same name (but different case) can exist in a table. For example, you could have Col1 and col1 . In this case, referencing a column by name must exactly match the case of the column name, or an exception will be thrown. For example, if the table myTable contains columns Col1 and columns col1 , you would refer to Col1 by name as myTable.Columns ["Col1"] , and reference col1 by name as myTable.Columns ["col1"] . Attempting to reference one of these columns in the form myTable.Columns ["COL1"] will generate an exception.
- If only one column, relationship, or constraint exists for a particular name, no case sensitivity applies. That is, if no other column, relationship, or constraint object in the table matches the name of that particular column, relationship, or constraint object, you can use any case to refer to the object by name without throwing an exception. For example, if there is only Col1 in the table, you can use my.Columns ["COL1"] for reference.
- Working with data in a DataTable
- After you create a DataTable in a DataSet, you can perform the same activities that you perform when working with tables in the database. You can add, view, edit, and delete data in the table; you can monitor errors and events; and you can query the data in the table. When modifying the data in the DataTable , you can also verify that the changes are correct and decide whether to accept or reject the changes programmatically.
- Constructor
- Initializes a new instance of the DataTable class.
- Overload this member. For complete information about this member, including syntax, usage, and examples, click the appropriate name in the overload list.
|
| |
---|---|---|
|
| |
|
| |
|
| |
|
|
- Attributes
|
| |
---|---|---|
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
|
- Explicit interface implementation
|
| |
---|---|---|
|
| |
|
| |
|
| |
|
|
- method
|
| |
---|---|---|
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
|
- Extension method
|
| |
---|---|---|
|
| |
|
|
- DataTable event
|
| |
---|---|---|
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
|
- DataTable dt = new DataTable ();
- dt.Columns.Add ("ID");
- dt.Columns.Add ("Name");
- DataRow dr = dt.NewRow ();
- object [] objs = {1, "Name"};
- dr.ItemArray = objs;
- dt.Rows.Add (dr);
- this.dataGridView1.DataSource = dt;
(1) CreateaDataTable DataTabledt = newDataTable ("Table_AX"); (2) AddcolumnsforDataTable // Method1 dt.Columns.Add ("column0", System.Type.GetType ("System.String")); // Method2 DataColumndc = newDataColumn ("column1", System.Type.GetType ("System .Boolean ")); dt.Columns.Add (dc); (3) AddrowsforDataTable // Initializetherow DataRowdr = dt.NewRow (); dr [" column0 "] =" AX "; dr [" column1 "] = true; dt.Rows.Add (dr); // Doesn'tinitializetherow DataRowdr1 = dt.NewRow (); dt.Rows.Add (dr1); (4) Selectrow // If Searchthesecondrow is not assigned, use isnull to select DataRow [] drs = dt.Select ("column1isnull"); DataRow [] drss = dt.Select ("column0 = 'AX'"); (5) CopyDataTableincludedata DataTabledtNew = dt.Copy (); (6) CopyDataTableonlyscheme DataTabledtOnlyScheme = dt.Clone (); (7) Operateonerow // operation on dt // Method1 DataRowdrOperate = dt.Rows [0]; drOperate ["column0 "] =" AXzhz "; drOperate [" column1 "] = false; // Method2 drOperate [0] =" AXzhz "; drOperate [1] = false; // Method3 dt.Rows [0] [" column0 "] = "AXzhz"; dt.Rows [0] ["column1"] = false; // Method4 dt.Rows [0] [0] = "AXzhz"; dt.Rows [0] [1] = false; (8) EvaluateanotherDataTable'srowtocurrentDatatable dtOnlyScheme.Rows.Add (dt.Rows [0] .ItemArray); (9) Converttostring System.IO.StringWritersw = newSystem.IO.StringWriter (); System.Xml.XmlTextWriterxw = newSystem.Xml.XmlTextWriter ( ); dt.WriteXml (xw); strings = sw.ToString (); (10) FilterDataTable dt.DefaultView.RowFilter = "column1 <> true"; dt.DefaultView.RowFilter = "column1 = true"; (11) Sortrow dt.DefaultView.Sort = "ID, NameASC"; dt = dt.DefaultView.ToTable (); (12) BindDataTable // The default binding is actually DefaultView gvTestDataTable.DataSource = dt; gvTestDataTable.Dat aBind (); (13) judgetheDataTable'sColumnnameisastring // Determine whether a string is the column name of the DataTable dtInfo.Columns.Contains ("AX"); (14) DataTableconverttoXMLandXMLconverttoDataTable protectedvoidPage_Load (objectsender, EventArgse) {DataTabledt_AX = newDataTable (); //dt_AX.Columns.Add("Sex",typeof(System.Boolean)); //DataRowdr=dt_AX.NewRow (); // dr ["Sex"] = true; //dt_AX.Rows.Add(dr ); stringxml = ConvertBetweenDataTableAndXML_AX (dt_AX); DataTabledt = ConvertBetweenDataTableAndXML_AX (xml);} publicstringConvertBetweenDataTableAndXML_AX (DataTabledtNeedCoveret) {System.IO.TextWritertw = newSystem.IO.StringWriter (); // ifTableName dtNeedCoveret.TableName = dtNeedCoveret.TableName.Length == 0? "Table_AX": dtNeedCoveret.TableName; dtNeedCoveret.WriteXml (tw); dtNeedCoveret.WriteXmlSchema (tw); returntw.ToString ();} PublicDataTableConvertBe { .TextReadertrDataTable = newSystem.IO.StringReader (xml.Substring (0, xml.IndexOf ("<? Xml"))); System.IO.TextReadertrSchema = newSystem.IO.StringReader (xml.Substring (xml.IndexOf ("< ? xml ")))); DataTabledtReturn = newDataTable (); dtReturn.ReadXmlSchema (trSchema); dtReturn.ReadXml (trDataTable); returndtReturn;}
- Copy all or part of the datatable structure to a new datatable
- datatable copy table structure: we can use the .clone () method;
DataTable oldDT = GetDataTable ();
DataTable newDT = oldDT.Clone ();
Copy all the information in the datatable to a new datatable, including the structure and data:
DataTable oldDT = GetDataTable ();
DataTable newDT = oldDT.Copy ();
Copy a row in the datatable: we can use the .ImportRow () method;
DataTable oldDT = GetDataTable ();
DataTable newDT = new DataTable ();
newDT.ImportRow (oldDT.Rows [1]); Copy the second row of data from the original datatable to the new datatable.