Was this page helpful?
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

DataTable Constructor (String)


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

Namespace:   System.Data
Assembly:  System.Data (in System.Data.dll)

public DataTable(
	string tableName


Type: System.String

The name to give the table. If tableName is null or an empty string, a default name is given when added to the DataTableCollection.

The following example creates a DataTable and displays it in a DataGridView control.

private void MakeDataTableAndDisplay()
    // Create new DataTable.
    DataTable table = new DataTable("table");

    // Declare DataColumn and DataRow variables.
    DataColumn column;
    DataRow row;

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

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

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

.NET Framework
Available since 1.1
Return to top
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
© 2015 Microsoft