Data Table
Data Table
The DataTable is a central object in the ADO.NET library. Other objects that use the DataTable include the DataSet and the DataView. If you are creating a DataTable programmatically, you must first define its schema by adding DataColumn objects to the DataColumnCollection (accessed through the Columns property). You can create a DataTable object by using the DataTable constructor, or by passing constructor arguments to the Add method of the Tables property of the DataSet, which is a DataTableCollection. You can also create DataTable objects within a DataSet by using the Fill or FillSchema methods of the DataAdapter object. When you first create a DataTable, it does not have a schema (a structure). To define the schema of the table, you must create and add DataColumn objects to the Columns collection of the table. After you have defined the schema for a DataTable, you can add rows of data to the table by adding DataRow objects to the Rows collection of the table. The DataRowCollection represents the actual DataRow objects in the DataTable, and the DataColumnCollection contains the DataColumn objects that describe the schema of the DataTable. The DataColumnCollection defines the schema of a DataTable, and determines what kind of data each DataColumn can contain. You can access the DataColumnCollection through the Columns property of the DataTable object. The DataRowCollection is a major component of the DataTable. While the DataColumnCollection defines the schema of the table, the DataRowCollection contains the actual data for the table, where each DataRow in the DataRowCollection represents a single row.
y private void CreateNewDataRow() { // Use the MakeTable function below to create a new table. DataTable table; table = MakeNamesTable(); // Once a table has been created, use the // NewRow to create a DataRow. DataRow row; row = table.NewRow(); // Then add the new row to the collection. row["fName"] = "John"; row["lName"] = "Smith"; table.Rows.Add(row); foreach(DataColumn column in table.Columns) Console.WriteLine(column.ColumnName); dataGrid1.DataSource=table; }
The DataTable class doesn't store anything at all in ViewState. The DataTable class is not specific for web applications and doesn't use any web specific features.
If any data from a DataTable object is stored in ViewState it's because you are using it as a data source for a web control that is storing information in ViewState, like for example a DataGrid. The control copies the information from the DataTable that it needs to recreate the control on postback. The DataTable object that was used to data bind the control initially doesn't exist any more once the page is sent to the browser. The Columns property of the DataGrid control (for example) is a collection of DataGridColumn objects, it's not the same as the Columns property of the DataTable class that is a collection of DataColumn objects, eventhough most of the information from the columns of the DataTable is copied to the columns of the DataGrid when the DataGrid is data bound.