A DataTable object resembles a database table and has a collection of DataColumns (i.e. fields) and DataRows (i.e. records).
It can also have a primary key based on one or more columns and a collection of Constraint objects which are useful for enforcing the uniqueness of the values in a column.
DataTable objects in a DataSet class are often tied to each other through relationships.
It is possible for a DataTable object to exist outside a DataSet class but it obviously cannot have any relationships.
The majority of applications will populate a DataTable directly from a database however it is possible to fill a datatable using code.

either created initially if defining primary keys
or implicitly using dataadapter.fill

Creating a DataTable Object

Dim objDataTable As System.Data.DataTable 
objDataTable = New System.Data.DataTable
objDataTable.TableName = "DataTableName"

Alternatively you could pass the name of the datatable string directly to the constructor

Dim objDataTable As New System.Data.DataTable("DataTableName") 

Retrieving a DataTable from a DataSet

Dim objDataTable As System.Data.DataTable 
objDataTable = objDataSet.Tables("DataTableName")
objDataTable = objDataSet.Tables(0)

Creating a Primary Key

This creates a primary key from a single column

objDataTable.PrimaryKey = obDataColumn 
objDataTable.PrimaryKey = objDataTable.Columns("UniqueID")

This creates a primary key from two columns

Dim aobjDataColumns() As DataColumn = {objDataColumn1, objDataColumn2} 
objDataTable.PrimaryKey = aobjDataColumns

Filtering, Searching and Sorting

There are two techniques you can use for filtering, searching and sorting the rows in a DataTable.
DataView object - This is explained in more detail on the DataView page.
objDataTable.Select - Takes up to three arguments, a filter expression, a sort criteria and a DataViewRowState enumerated value.

This performs a simple filter and returns an array of matching DataRow objects.

Dim objDataRows() As System.Data.DataRow 
objDataRows = objDataTable.Select("FirstName = 'Russell'")

This performs a simple filter and sorts the matching DataRow objects by Age

objDataRows = objDataTable.Select("FirstName = 'Russell',  "Age DESC")  

This performs a simple filter and sort but only returns the DataRow objects that have been deleted.
The default DataViewRowState is CurrentRows.

objDataRows = objDataTable.Select("FirstName = 'Russell',  "Age DESC", DataViewRowState.Deleted)  


CaseSensitiveReturns True if string comparisons in this DataTable are case sensitive.
ChildRelationsReturns the collection of DataRelation objects in which this DataTable is the master table.
ColumnsReturns the collection of chold DataColumn objects
ConstraintsReturns the collection of the Constraint objects for this DataTable (e.g. foreign key or unique constraints).
DataSetReturns the DataSet this DataTable belongs to.
DefaultViewReturns the DefaultView object that you can use to filter and sort the DataTable.
DisplayExpressionA string expression used to represent this table in the user interface. The expression supports the same syntax defined for the DataColumn's Expression property.
ExtendedPropertiesReturns the PropertyCollection object used to store custom information about this DataTable.
HasErrorsReturns True if ther are errors in any of the DataRow objects in this DataTable.
LocaleThe CultureInfo object containing the locale information used to compare strings in the DataTable.
MinimumCapacityThe initial number of rows for this DataTable. Useful if you are going to manually populate a DataTable.
NamespaceThe namespace for this DataTable used when importing or exporting XML data.
ParentRelationsReturns the collection of the DataRelation objects in which this DataTable is the detail table.
PrefixThe XML prefix for the DataTable namespace.
PrimaryKeyAn array of DataColumn objects that represent the primary keys for the DataTable.
RowsReturns the collection of child DataRow objects
TableNameThe name of this DataTable object.


AcceptChangesCommits all changes to this DataTable after it was loaded or since the most recent AcceptChanges method.
BeginLoadDataTurns off notifications, index maintenance, and constraints while loading data; to be used in conjunction with the EndLoadData and LoadDataRow methods.
ClearClears all the data in the DataTable.
CloneCreates a cloned DataTable that contains the identical structure, tables and relationships as the current one.
ComputeCalculates the expression specified by the first argument for all the rows that satisfy the filter expression specified in the second argument.
CopyCreates a DataTable that has both the same structure and the same data as the current one.
EndLoadDataEnds a load data operation started with BeginLoadData.
GetChangesGets a DataTable that contains all the changes made to the current one since it was loaded or since the most recent AcceptChanges method, optionally filtered using the DataRowState argument.
GetErrorsReturns the array of all the DataRow objects that have errors.
ImportRowCopies the DataRow passed as an argument into the current DataTable. The row retains its original and current values, its DataRowState values, and its errors.
LoadDataRowFinds and updates a specific row or creates a new row if no matching row is found.
NewRowCreates a DataRow with the same schema as the current DataTable.
RejectChangesRejects all changes to this DataTable after it as loaded or since the most recent AcceptChanges method.
ResetResets the DataTable to its original state.
SelectReturns an array of all the DataRow objects that satify the filter expression. It takes optional arguments that specify the desired sort order and the DataViewRowState to be matched.


ColumnChangedFires after a DataColumn has changed.
ColumnChangingFires when a DataColumn is changing. The event handler can inspect the value.
RowChangedFires after a DataRow has changed.
RowChangingFires when a DataRow is changing.
RowDeletedFires after a DataRow has been deleted.
RowDeletingFires when a DataRow is being deleted.

© 2020 Better Solutions Limited. All Rights Reserved. © 2020 Better Solutions Limited TopPrevNext