The DataTable class provides a consistent programming model regardless of the actual data source. The DataTable contains DataColumn and Constraint objects that define the schema of the data represented. The actual data is stored as a collection of DataRows within the DataTable.
The schema of the DataTable can be created entirely programmatically, retrieved as the result of a query against a data source using a .NET managed data provider or loaded from an XML document or stream through the DataSet the DataTable belongs to.
The data in the DataTable can be populated from the data source, modified, and later reconciled back to a data source using a DataAdapter. Alternatively, the data can be populated from XML documents or streams, modified, and saved as an XML document or stream through the DataSet the DataTable belongs to. Finally, the data stored in the DataTable can be manipulated programmatically.
There are similarities between the DataTable and the DataReader in that they both can store the results of a query, which can then be accessed through row and column objects. The primary difference is that the DataTable is a disconnected class that places little restriction on how the data within it is accessed and allows that data to be filtered, sorted, and modified; the DataReader is a connected class that provides little functionality beyond forward-only, read-only access to the result set, one row at a time.
The commonly used public properties of the DataTable class are listed in Table 23-1.
Property |
Description |
---|---|
CaseSensitive |
Gets or sets a value indicating whether string comparisons within the table are treated as case-sensitive. |
DataSet |
Gets a reference to the DataSet the table belongs to. |
DefaultView |
Gets the default DataView that is associated with the table. |
DisplayExpression |
Gets or sets the expression that represents the table in the user interface. |
HasErrors |
Gets a value that indicates whether there are errors in any rows of the table. |
Locale |
Gets or sets locale information that's used to compare strings stored in the table.. |
MinimumCapacity |
Gets or sets the initial number of rows of the table. |
Namespace |
Gets or sets the XML namespace for the XML representation of the data stored in the table. |
Prefix |
Gets or sets the XML prefix that aliases the namespace of the table. |
TableName |
Gets or sets the name of the table. |
The commonly used public collections of the DataTable class are listed in Table 23-2.
Collection |
Description |
---|---|
ChildRelations |
Accesses the DataRelationCollection that contains the child relations for the table as a collection of DataRelation objects. |
Columns |
Accesses the DataColumnCollection that contains the columns for the table as a collection of DataColumn objects. |
Constraints |
Accesses the ConstraintCollection that contains the constraints for the table as a collection of Constraint objects. |
ExtendedProperties |
Accesses the PropertyCollection that contains the extended properties for the table as a collection of key-and-value pairs. |
ParentRelations |
Accesses the DataRelationCollection that contains the parent relations for the table as a collection of DataRelation objects. |
PrimaryKey |
Accesses the array of DataColumn objects that make up the primary key of the table. |
Rows |
Accesses the DataRowCollection that contains the rows of data in the table as a collection of DataRow objects. |
The commonly used public methods of the DataTable class are listed in Table 23-3.
Method |
Description |
---|---|
AcceptChanges( ) |
Commits all changes made to the table since the last time it was loaded or since the last time changes were committed. |
BeginLoadData( ) |
Turns off constraints, notifications, and index maintenance while loading data. |
Clear( ) |
Removes all data rows from the table. |
Clone( ) |
Creates a new table with the same schema as the original but contains none of the data. |
Compute( ) |
Returns the result of an aggregate expression on a subset of rows meeting the filter criteria. |
Copy( ) |
Creates a new table with the same schema and data as the original. |
EndLoadData( ) |
Turns on constraints, notifications, and index maintenance after loading data. |
GetChanges( ) |
Gets a copy of the changes made to a table since the last time it was loaded or since the last time changes were committed. |
GetErrors( ) |
Gets an array of rows that contain errors. |
ImportRow( ) |
Copies a specified row into the table. |
LoadDataRow( ) |
Finds a row in the table and updates the values with the supplied array, or if the row isn't found, adds a new row to the table. |
NewRow( ) |
Returns a new row with the same schema as the table. |
RejectChanges( ) |
Rejects all changes made to the table since the last time it was loaded or the last time changes were committed. |
Reset( ) |
Discards the contents of the table, resetting it to an uninitialized state. |
Select( ) |
Returns an array of rows matching optionally specified filter and DataRowViewState criteria, optionally sorted according to specified criteria. |
The commonly used public events of the DataTable class are listed in Table 23-4.
Event |
Description |
---|---|
ColumnChanged |
Raised when a value has been changed for a column in a row. |
ColumnChanging |
Raised when a value is being changed for a column in a row. |
RowChanged |
Raised after a row has been successfully changed. |
RowChanging |
Raised when a row is about to be changed. |
RowDeleted |
Raised after a row has been deleted from the table. |
RowDeleting |
Raised when a row is about to be deleted from the table. |
The DataTable class is contained within the System.Data namespace. The DataTable class inherits from MarshalByValueComponent and implements the IListSource, ISupportInitialize, and ISerializable interfaces. It is safe for multithreaded read operations; multithreaded write operations must be synchronized. DataTable objects can be passed between different application domains.