DataTable 构造函数

定义

初始化 DataTable 类的新实例。

重载

DataTable()

在不使用参数的情况下初始化 DataTable 类的新实例。

DataTable(String)

使用指定的表名初始化 DataTable 类的新实例。

DataTable(SerializationInfo, StreamingContext)
已过时.

用序列化数据初始化 DataTable 类的新实例。

DataTable(String, String)

使用指定的表名和命名空间初始化 DataTable 类的新实例。

DataTable()

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

在不使用参数的情况下初始化 DataTable 类的新实例。

C#
public DataTable ();

示例

以下示例使用 和 创建新的 DataTable ,并将其显示在 控件中DataGridViewDataRowDataColumn

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

注解

构造函数为对象的所有属性 DataTable 设置初始值。 下表显示了属性及其默认值。 创建 实例 DataTable 时,以下读/写属性设置为初始值。

properties 默认值
CaseSensitive 与父 DataSet级 相同(如果它属于父级 )。 否则为 false
DisplayExpression 空字符串 (“”)
区域设置 与属性) 返回的父DataSet对象的 (相同;如果没有父对象,则默认值为当前系统 CultureInfoCultureInfoLocale
MinimumCapacity 50 行。

可以通过单独调用 属性来更改其中任何属性的值。

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.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

使用指定的表名初始化 DataTable 类的新实例。

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

参数

tableName
String

要向表提供的名称。 如果 tableNamenull 或是空字符串,则在添加到 DataTableCollection 中时指定默认名称。

示例

以下示例创建 并将其 DataTable 显示在 控件中 DataGridView

C#
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 9 和其他版本
产品 版本
.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

注意

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

用序列化数据初始化 DataTable 类的新实例。

C#
protected DataTable (System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context);
C#
[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);

参数

info
SerializationInfo

数据集的序列化数据。

context
StreamingContext

有关序列化流的上下文信息。

属性

例外

仅限 .NET 7 及更高版本: info 包含二进制数据。

注解

构造函数的 DataTable 此实现对于 是必需的 ISerializable

另请参阅

适用于

.NET 9 和其他版本
产品 版本 (已过时)
.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

使用指定的表名和命名空间初始化 DataTable 类的新实例。

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

参数

tableName
String

要向表提供的名称。 如果 tableNamenull 或是空字符串,则在添加到 DataTableCollection 中时指定默认名称。

tableNamespace
String

DataTable 中所存储数据的 XML 表示形式的命名空间。

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.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