NGWS SDK Documentation  

This is preliminary documentation and subject to change.
To comment on this topic, please send us email at ngwssdk@microsoft.com. Thanks!

DataTable Class

Represents one table of in-memory data.

Object
   Component
      DataTable

[Visual Basic]
Public Class DataTable
   Inherits Component
   Implements IListSource
[C#]
public class DataTable : Component, IListSource
[C++]
public __gc class DataTable : public Component, IListSource
[JScript]
public class DataTable extends Component, IListSource

Remarks

The DataTable is a central object in the ADO+ library. Other objects which 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 ColumnsCollection (accessed through the Columns property). Only after all columns have been added can you then begin to add DataRow objects to the RowsCollection (accessed through the Rows property).

Note   To add rows to a table, you must first use the NewRow method to return a new DataRow object. The NewRow method returns a row with the table's schema, as it is defined by the table's columns.

The schema of a table is defined by the ColumnsCollection, the collection of DataColumn objects. The ColumnsCollection is accessed through the Columns property. A DataTable must have at least one column to qualify as a table. See the DataColumn and ColumnsCollection for more information about defining a schema for the table.

The DataTable contains a collection of Constraint objects that can be used to ensure the integrity of the data.

The System.WinForms.DataGrid control can display one table at a time. The DataTable of the currently displayed table can be accessed through the System.WinForms.DataGrid.DataGridTable property.

When an instance of DataTable is created, some of the read/write properties are set to initial values. For a list of these values, see the DataTable constructor.

Requirements

Namespace: System.Data

Assembly: System.Data.dll

Example [Visual Basic]

The following example creates two DataTable objects, two DataRelation objects, and adds the new objects to a DataSet.

[Visual Basic]

' Put the next lines into the Declarations section.
Private myDataSet As DataSet
Private parentTable As DataTable
Private childTable As DataTable

Private Sub MakeDataSet()
   MakeParentTable
   MakeChildTable
   MakeDataRelation
End Sub

Private Sub MakeParentDataTable()
   ' Create a new DataTable.
   Dim myDataTable As New DataTable
   Dim myDataColumn As DataColumn
   Dim myDataRow As DataRow

   ' Create new DataColumn, set DataType, ColumnName and add to DataTable.    
   myDataColumn = New DataColumn
   With myDataColumn
      .DataType = System.Type.GetType("System.Int32")
      .ColumnName = "id"
      .AutoIncrement = True
      .AutoIncrementSeed = 1
      .AutoIncrementStep = 1
      .Caption = "ID"
      .ReadOnly = True
      .Unique = True
   End With

   ' Add the Column to the ColumnsCollection.
   myDataTable.Columns.Add(myDataColumn)

   ' Make the ID column the primary key column.
   Dim cols() As DataColumn
   cols(0) = myDataColumn
   myDataTable.PrimaryKey = cols
   
   ' Create second column.
   myDataColumn = New DataColumn
   With myDataColumn
      .DataType = System.Type.GetType("System.String")
      .ColumnName = "Item"
      .AutoIncrement = False
      .Caption = "Item"
      .ReadOnly = False
      .Unique = False
   End With
   myDataTable.Columns.Add(myDataColumn)
   ' Create new DataRow objects and add to DataTable.    
   Dim i As Integer

   For i = 0 To 4
      myDataRow = myDataTable.NewRow
      myDataRow("id") = i
      myDataRow("Item") = "Item " & i
      myDataTable.Rows.Add myDataRow
   Next i

   myDataSet = New DataSet
   myDataSet.Tables.Add(myDataTable)
End Sub

Private Sub MakeChildDataTable()
   ' Create a new DataTable.
   Dim myDataTable As New DataTable
   Dim myDataColumn As DataColumn
   Dim myDataRow As DataRow

   ' Create new DataColumn, set DataType, ColumnName and add to DataTable.    
   myDataColumn = New DataColumn
   With myDataColumn
      .DataType = System.Type.GetType("System.Int32")
      .ColumnName = "ChildID"
      .AutoIncrement = True
      .AutoIncrementSeed = 1
      .AutoIncrementStep = 1
      .Caption = "ID"
      .ReadOnly = True
      .Unique = True
   End With

   ' Add the Column to the ColumnsCollection.
   myDataTable.Columns.Add(myDataColumn)

   ' Make the ID column the primary key column.
   Dim cols() As DataColumn
   cols(0) = myDataColumn
   myDataTable.PrimaryKey = cols
   
   ' Create second column.
   myDataColumn = New DataColumn
   With myDataColumn
      .DataType = System.Type.GetType("System.String")
      .ColumnName = "ChildItem"
      .AutoIncrement = False
      .Caption = "ChildItem"
      .ReadOnly = False
      .Unique = False
   End With

   myDataTable.Columns.Add(myDataColumn)

   ' Create third column.
   myDataColumn = New DataColumn
   With myDataColumn
      .DataType = System.Type.GetType("System.Int32")
      .ColumnName = "ParentID"
      .AutoIncrement = False
      .Caption = "ParentID"
      .ReadOnly = False
      .Unique = False
   End With

   myDataTable.Columns.Add(myDataColumn)

   ' Create new DataRow objects and add to DataTable.    
   Dim i As Integer
   Dim j As Integer
   For i = 0 To 20
      myDataRow = myDataTable.NewRow
      myDataRow("childID") = i
      myDataRow("Item") = "Item " & i
      myDataTable.Rows.Add(myDataRow)

   Next i
   ' Instantiate the DataSet and add the new DataTable.
   myDataSet = New DataSet
   myDataSet.Tables.Add(myDataTable)
End Sub

See Also

DataTable Members | System.Data Namespace | ColumnsCollection | DataColumn | DataRow | DataSet | DataView | RowsCollection