DataTable Constructors

Definition

Initializes a new instance of the DataTable class.

Overloads

DataTable()

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

DataTable(String)

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

DataTable(SerializationInfo, StreamingContext)
Obsolete.

Initializes a new instance of the DataTable class with serialized data.

DataTable(String, String)

Initializes a new instance of the DataTable class using the specified table name and namespace.

DataTable()

Source:
DataTable.cs
Source:
DataTable.cs
Source:
DataTable.cs

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

public DataTable ();

Examples

The following example creates a new DataTable with DataColumn and DataRow, and displays it in a DataGridView control.

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

    // 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;
}

Remarks

The constructor sets initial values for all properties of the DataTable object. The following table shows the properties and their default values. When an instance of DataTable is created, the following read/write properties are set to initial values.

Property Default value
CaseSensitive Same as the parent DataSet, if it belongs to one. Otherwise, false.
DisplayExpression Empty string ("")
Locale Same as the parent DataSet object's CultureInfo (returned by the Locale property); if no parent exists, the default is the current system CultureInfo.
MinimumCapacity 50 rows.

You can change the value for any of these properties through a separate call to the property.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

DataTable(String)

Source:
DataTable.cs
Source:
DataTable.cs
Source:
DataTable.cs

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

public DataTable (string? tableName);
public DataTable (string tableName);

Parameters

tableName
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.

Examples

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;
}

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

DataTable(SerializationInfo, StreamingContext)

Source:
DataTable.cs
Source:
DataTable.cs
Source:
DataTable.cs

Caution

This API supports obsolete formatter-based serialization. It should not be called or extended by application code.

Initializes a new instance of the DataTable class with serialized data.

protected DataTable (System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context);
[System.Obsolete("This API supports obsolete formatter-based serialization. It should not be called or extended by application code.", DiagnosticId="SYSLIB0051", UrlFormat="https://aka.ms/dotnet-warnings/{0}")]
protected DataTable (System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context);

Parameters

info
SerializationInfo

The serialized data for the data set.

context
StreamingContext

Contextual information about the serialized stream.

Attributes

Exceptions

.NET 7 and later versions only: info contains binary data.

Remarks

This implementation of the DataTable constructor is required for ISerializable.

See also

Applies to

.NET 9 and other versions
Product Versions (Obsolete)
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7 (8, 9)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

DataTable(String, String)

Source:
DataTable.cs
Source:
DataTable.cs
Source:
DataTable.cs

Initializes a new instance of the DataTable class using the specified table name and namespace.

public DataTable (string? tableName, string? tableNamespace);
public DataTable (string tableName, string tableNamespace);

Parameters

tableName
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.

tableNamespace
String

The namespace for the XML representation of the data stored in the DataTable.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1