Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

DataTable.DataTable() Constructor

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

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

public DataTable ()
public DataTable ()
public function DataTable ()
Not applicable.

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.

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

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.