Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

DataTable Constructor

.NET Framework 1.1

Initializes a new instance of the DataTable class.

Overload List

Initializes a new instance of the DataTable class with no arguments.

Supported by the .NET Compact Framework.

[Visual Basic] Public Sub New()
[C#] public DataTable();
[C++] public: DataTable();
[JScript] public function DataTable();

Intitalizes a new instance of the DataTable class with the specified table name.

Supported by the .NET Compact Framework.

[Visual Basic] Public Sub New(String)
[C#] public DataTable(string);
[C++] public: DataTable(String*);
[JScript] public function DataTable(String);

Initializes a new instance of the DataTable class with the SerializationInfo and the StreamingContext.

[Visual Basic] Protected Sub New(SerializationInfo, StreamingContext)
[C#] protected DataTable(SerializationInfo, StreamingContext);
[C++] protected: DataTable(SerializationInfo*, StreamingContext);
[JScript] protected function DataTable(SerializationInfo, StreamingContext);

Example

[Visual Basic, C#, C++] The following example creates a System.Windows.Forms.DataGridTable and displays it in a System.Windows.Forms.DataGrid control.

[Visual Basic, C#, C++] Note   This example shows how to use one of the overloaded versions of the DataTable constructor. For other examples that might be available, see the individual overload topics.
[Visual Basic] 
Private Sub MakeDataTableAndDisplay()
   ' Create new DataTable.
   Dim myDataTable As DataTable = New DataTable("MyDataTable")

   ' Declare DataColumn and DataRow variables.
   Dim myDataColumn As DataColumn
   Dim myDataRow As DataRow

   ' Create new DataColumn, set DataType, ColumnName and add to DataTable.    
   myDataColumn = New DataColumn
   myDataColumn.DataType = System.Type.GetType("System.Int32")
   myDataColumn.ColumnName = "id"
   myDataTable.Columns.Add(myDataColumn)

   ' Create second column.
   myDataColumn = New DataColumn
   myDataColumn.DataType = Type.GetType("System.String")
   myDataColumn.ColumnName = "item"
   myDataTable.Columns.Add(myDataColumn)

   ' Create new DataRow objects and add to DataTable.    
   Dim i As Integer
   For i = 0 To 10
      myDataRow = myDataTable.NewRow
      myDataRow("id") = i
      myDataRow("item") = "item " & i
      myDataTable.Rows.Add(myDataRow)
   Next i

   ' Set to DataGrid.DataSource property to the table.
   DataGrid1.DataSource = myDataTable
End Sub

[C#] 
private void MakeDataTableAndDisplay(){
   // Create new DataTable.
   DataTable myDataTable = new DataTable("MyDataTable");
   
   // Declare DataColumn and DataRow variables.
   DataColumn myDataColumn;
   DataRow myDataRow;

   // Create new DataColumn, set DataType, ColumnName and add to DataTable.    
   myDataColumn = new DataColumn();
   myDataColumn.DataType = System.Type.GetType("System.Int32");
   myDataColumn.ColumnName = "id";
   myDataTable.Columns.Add(myDataColumn);

   // Create second column.
   myDataColumn = new DataColumn();
   myDataColumn.DataType = Type.GetType("System.String");
   myDataColumn.ColumnName = "item";
   myDataTable.Columns.Add(myDataColumn);

   // Create new DataRow objects and add to DataTable.    
   for(int i = 0; i < 10; i++){
      myDataRow = myDataTable.NewRow();
      myDataRow["id"] = i;
      myDataRow["item"] = "item " + i;
      myDataTable.Rows.Add(myDataRow);
   }
   // Set to DataGrid.DataSource property to the table.
   dataGrid1.DataSource = myDataTable;
}

[C++] 
private:
 void MakeDataTableAndDisplay(){
    // Create new DataTable.
    DataTable* myDataTable = new DataTable(S"MyDataTable");
    
    // Declare DataColumn and DataRow variables.
    DataColumn* myDataColumn;
    DataRow* myDataRow;
 
    // Create new DataColumn, set DataType, ColumnName and add to DataTable.    
    myDataColumn = new DataColumn();
    myDataColumn->DataType = System::Type::GetType(S"System.Int32");
    myDataColumn->ColumnName = S"id";
    myDataTable->Columns->Add(myDataColumn);
 
    // Create second column.
    myDataColumn = new DataColumn();
    myDataColumn->DataType = Type::GetType(S"System.String");
    myDataColumn->ColumnName = S"item";
    myDataTable->Columns->Add(myDataColumn);
 
    // Create new DataRow objects and add to DataTable.    
    for(int i = 0; i < 10; i++){
       myDataRow = myDataTable->NewRow();
       myDataRow->Item[S"id"] = __box(i);
       myDataRow->Item[S"item"] = String::Format( S"item {0}", __box(i));
       myDataTable->Rows->Add(myDataRow);
    }
    // Set to DataGrid.DataSource property to the table.
    dataGrid1->DataSource = myDataTable;
 }

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

See Also

DataTable Class | DataTable Members | System.Data Namespace

Show:
© 2015 Microsoft