System.Data.DataTable
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
tables("").beginloaddata
adapter.fill
tables("").endloaddata
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)
Properties
CaseSensitive | Returns True if string comparisons in this DataTable are case sensitive. |
ChildRelations | Returns the collection of DataRelation objects in which this DataTable is the master table. |
Columns | Returns the collection of chold DataColumn objects |
Constraints | Returns the collection of the Constraint objects for this DataTable (e.g. foreign key or unique constraints). |
Container | |
DataSet | Returns the DataSet this DataTable belongs to. |
DefaultView | Returns the DefaultView object that you can use to filter and sort the DataTable. |
DesignMode | |
DisplayExpression | A string expression used to represent this table in the user interface. The expression supports the same syntax defined for the DataColumn's Expression property. |
ExtendedProperties | Returns the PropertyCollection object used to store custom information about this DataTable. |
HasErrors | Returns True if ther are errors in any of the DataRow objects in this DataTable. |
Locale | The CultureInfo object containing the locale information used to compare strings in the DataTable. |
MinimumCapacity | The initial number of rows for this DataTable. Useful if you are going to manually populate a DataTable. |
Namespace | The namespace for this DataTable used when importing or exporting XML data. |
ParentRelations | Returns the collection of the DataRelation objects in which this DataTable is the detail table. |
Prefix | The XML prefix for the DataTable namespace. |
PrimaryKey | An array of DataColumn objects that represent the primary keys for the DataTable. |
Rows | Returns the collection of child DataRow objects |
Site | |
TableName | The name of this DataTable object. |
Methods
AcceptChanges | Commits all changes to this DataTable after it was loaded or since the most recent AcceptChanges method. |
BeginInit | |
BeginLoadData | Turns off notifications, index maintenance, and constraints while loading data; to be used in conjunction with the EndLoadData and LoadDataRow methods. |
Clear | Clears all the data in the DataTable. |
Clone | Creates a cloned DataTable that contains the identical structure, tables and relationships as the current one. |
Compute | Calculates the expression specified by the first argument for all the rows that satisfy the filter expression specified in the second argument. |
Copy | Creates a DataTable that has both the same structure and the same data as the current one. |
Dispose | |
EndInit | |
EndLoadData | Ends a load data operation started with BeginLoadData. |
GetChanges | Gets 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. |
GetErrors | Returns the array of all the DataRow objects that have errors. |
GetService | |
GetType | |
ImportRow | Copies the DataRow passed as an argument into the current DataTable. The row retains its original and current values, its DataRowState values, and its errors. |
LoadDataRow | Finds and updates a specific row or creates a new row if no matching row is found. |
NewRow | Creates a DataRow with the same schema as the current DataTable. |
RejectChanges | Rejects all changes to this DataTable after it as loaded or since the most recent AcceptChanges method. |
Reset | Resets the DataTable to its original state. |
Select | Returns 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. |
Events
ColumnChanged | Fires after a DataColumn has changed. |
ColumnChanging | Fires when a DataColumn is changing. The event handler can inspect the value. |
RowChanged | Fires after a DataRow has changed. |
RowChanging | Fires when a DataRow is changing. |
RowDeleted | Fires after a DataRow has been deleted. |
RowDeleting | Fires when a DataRow is being deleted. |
© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext