Indicates a table of data in the memory.
For a list of all members of this type, see datatable members.
System. Object
System. componentmodel. externalbyvaluecomponent
System. Data. datatable
[Visual Basic]<Serializable>Public Class DataTable Inherits MarshalByValueComponent Implements IListSource, ISupportInitialize, ISerializable[C#][Serializable]public class DataTable : MarshalByValueComponent, IListSource, ISupportInitialize, ISerializable[C++][Serializable]public __gc class DataTable : public MarshalByValueComponent, IListSource, ISupportInitialize, ISerializable[JScript]public Serializableclass DataTable extends MarshalByValueComponent implements IListSource, ISupportInitialize, ISerializable
Thread Security
This type is safe for multi-threaded read operations. You must synchronize any write operations.
Remarks
DatatableIs the core object in the ADO. Net Library. Other usageDatatableThe objects include dataset and dataview.
When accessingDatatableObjects are case sensitive. For example, ifDatatableIt is named "mydatatable" and the other is named "mydatatable". strings used to search one of the tables are considered case sensitive. However, if "mydatatable" exists and "mydatatable" does not exist, the search string is case-insensitive. UsageDatatableFor more information about the object, see .
If you are creatingDatatableYou must first add the datacolumn object to datacolumncollection (accessed through the columns attribute) to define its architecture. About AddingDatacolumnFor more information about the object, see .
ToDatatableThe newrow method must be used to return the new datarow object.NewrowMethod return hasDatatableThe schemaDatacolumncollectionAs defined.DatatableThe maximum number of lines that can be stored is 16,777,216. For more information, see .
Table ArchitectureDatacolumnObject setDatacolumncollectionDefinition. PassColumnsAttribute accessDatacolumncollection. For more information about defining the schema of a table, seeDatacolumnAndDatacolumncollection.
DatatableContains a set of constraint objects that can be used to ensure data integrity. For more information, see .
To determine when to change a table, use one of the following events: rowchanged, rowchanging, rowdeleting, and rowdeleted. For more information, see .
WhenDatatableInstance, some read/write attributes are set to the initial value. For a list of these values, seeDatatableConstructor.
Note:DatasetAndDatatableThe object is inherited from marshalbyvaluecomponent and supports the iserializable interface for remote processing. These are only ADO. Net objects that can be processed remotely.
Example
[C #]
// Put the next line into the declarations section.
Private system. Data. dataset mydataset;
Private void makedatatables (){
// Run all of the functions.
Makeparenttable ();
Makechildtable ();
Makedatarelation ();
Bindtodatagrid ();
}
Private void makeparenttable (){
// Create a new datatable.
System. Data. datatable mydatatable = new datatable ("parenttable ");
// Declare variables for datacolumn and datarow objects.
Datacolumn mydatacolumn;
Datarow mydatarow;
// Create new datacolumn, set datatype, columnname and add to datatable.
Mydatacolumn = new datacolumn ();
Mydatacolumn. datatype = system. type. GetType ("system. int32 ");
Mydatacolumn. columnname = "ID ";
Mydatacolumn. readonly = true;
Mydatacolumn. Unique = true;
// Add the column to the datacolumncollection.
Mydatatable. Columns. Add (mydatacolumn );
// Create second column.
Mydatacolumn = new datacolumn ();
Mydatacolumn. datatype = system. type. GetType ("system. String ");
Mydatacolumn. columnname = "parentitem ";
Mydatacolumn. autoincrement = false;
Mydatacolumn. Caption = "parentitem ";
Mydatacolumn. readonly = false;
Mydatacolumn. Unique = false;
// Add the column to the table.
Mydatatable. Columns. Add (mydatacolumn );
// Make the ID column the primary key column.
Datacolumn [] primarykeycolumns = new datacolumn [1];
Primarykeycolumns [0] = mydatatable. Columns ["ID"];
Mydatatable. primarykey = primarykeycolumns;
// Instantiate the dataset variable.
Mydataset = new dataset ();
// Add the new datatable to the dataset.
Mydataset. Tables. Add (mydatatable );
// Create three new datarow objects and add them to the datatable
For (INT I = 0; I <= 2; I ++ ){
Mydatarow = mydatatable. newrow ();
Mydatarow ["ID"] = I;
Mydatarow ["parentitem"] = "parentitem" + I;
Mydatatable. Rows. Add (mydatarow );
}
}
Private void makechildtable (){
// Create a new datatable.
Datatable mydatatable = new datatable ("childtable ");
Datacolumn mydatacolumn;
Datarow mydatarow;
// Create first column and add to the datatable.
Mydatacolumn = new datacolumn ();
Mydatacolumn. datatype = system. type. GetType ("system. int32 ");
Mydatacolumn. columnname = "childid ";
Mydatacolumn. autoincrement = true;
Mydatacolumn. Caption = "ID ";
Mydatacolumn. readonly = true;
Mydatacolumn. Unique = true;
// Add the column to the datacolumncollection.
Mydatatable. Columns. Add (mydatacolumn );
// Create second column.
Mydatacolumn = new datacolumn ();
Mydatacolumn. datatype = system. type. GetType ("system. String ");
Mydatacolumn. columnname = "childitem ";
Mydatacolumn. autoincrement = false;
Mydatacolumn. Caption = "childitem ";
Mydatacolumn. readonly = false;
Mydatacolumn. Unique = false;
Mydatatable. Columns. Add (mydatacolumn );
// Create third column.
Mydatacolumn = new datacolumn ();
Mydatacolumn. datatype = system. type. GetType ("system. int32 ");
Mydatacolumn. columnname = "parentid ";
Mydatacolumn. autoincrement = false;
Mydatacolumn. Caption = "parentid ";
Mydatacolumn. readonly = false;
Mydatacolumn. Unique = false;
Mydatatable. Columns. Add (mydatacolumn );
Mydataset. Tables. Add (mydatatable );
// Create three sets of datarow objects, five rows each, and add to datatable.
For (INT I = 0; I <= 4; I ++ ){
Mydatarow = mydatatable. newrow ();
Mydatarow ["childid"] = I;
Mydatarow ["childitem"] = "item" + I;
Mydatarow ["parentid"] = 0;
Mydatatable. Rows. Add (mydatarow );
}
For (INT I = 0; I <= 4; I ++ ){
Mydatarow = mydatatable. newrow ();
Mydatarow ["childid"] = I + 5;
Mydatarow ["childitem"] = "item" + I;
Mydatarow ["parentid"] = 1;
Mydatatable. Rows. Add (mydatarow );
}
For (INT I = 0; I <= 4; I ++ ){
Mydatarow = mydatatable. newrow ();
Mydatarow ["childid"] = I + 10;
Mydatarow ["childitem"] = "item" + I;
Mydatarow ["parentid"] = 2;
Mydatatable. Rows. Add (mydatarow );
}
}
Private void makedatarelation (){
// Datarelation requires two datacolumn (Parent and Child) and a name.
Datarelation mydatarelation;
Datacolumn parentcolumn;
Datacolumn childcolumn;
Parentcolumn = mydataset. Tables ["parenttable"]. Columns ["ID"];
Childcolumn = mydataset. Tables ["childtable"]. Columns ["parentid"];
Mydatarelation = new datarelation ("parent2child", parentcolumn, childcolumn );
Mydataset. Tables ["childtable"]. parentrelations. Add (mydatarelation );
}
Private void bindtodatagrid (){
// Instruct the DataGrid to bind to the dataset, with
// Parenttable as the topmost datatable.
Datagrid1.setdatabinding (mydataset, "parenttable ");
}