DataTable Constructor (String)

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
)

Parameters

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";
    table.Columns.Add(column);

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

    // 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;
        table.Rows.Add(row);
    }
    // Set to DataGrid.DataSource property to the table.
    dataGrid1.DataSource = table;
}

.NET Framework
Available since 1.1
Return to top
Show:
© 2016 Microsoft