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 类的新实例。

public:
 DataTable();
public DataTable ();
Public Sub New ()

示例

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

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;
}
Private Sub MakeDataTableAndDisplay()
   ' Create new DataTable.
   Dim table As New DataTable

   ' Declare DataColumn and DataRow variables.
   Dim column As DataColumn
   Dim row As DataRow

   ' 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.    
   Dim i As Integer
   For i = 0 To 10
      row = table.NewRow
      row("id") = i
      row("item") = "item " & i
      table.Rows.Add(row)
   Next i

   ' Set to DataGrid.DataSource property to the table.
   DataGrid1.DataSource = table
End Sub

注解

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

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

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

另请参阅

适用于

DataTable(String)

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

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

public:
 DataTable(System::String ^ tableName);
public DataTable (string? tableName);
public DataTable (string tableName);
new System.Data.DataTable : string -> System.Data.DataTable
Public Sub New (tableName As String)

参数

tableName
String

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

示例

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

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;
}
Private Sub MakeDataTableAndDisplay()
   ' Create new DataTable.
   Dim table As New DataTable("table")

   ' Declare DataColumn and DataRow variables.
   Dim column As DataColumn
   Dim row As DataRow

   ' 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.    
   Dim i As Integer
   For i = 0 To 10
      row = table.NewRow
      row("id") = i
      row("item") = "item " & i
      table.Rows.Add(row)
   Next i

   ' Set to DataGrid.DataSource property to the table.
   DataGrid1.DataSource = table
End Sub

另请参阅

适用于

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 类的新实例。

protected:
 DataTable(System::Runtime::Serialization::SerializationInfo ^ info, System::Runtime::Serialization::StreamingContext context);
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);
new System.Data.DataTable : System.Runtime.Serialization.SerializationInfo * System.Runtime.Serialization.StreamingContext -> System.Data.DataTable
[<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}")>]
new System.Data.DataTable : System.Runtime.Serialization.SerializationInfo * System.Runtime.Serialization.StreamingContext -> System.Data.DataTable
Protected Sub New (info As SerializationInfo, context As StreamingContext)

参数

info
SerializationInfo

数据集的序列化数据。

context
StreamingContext

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

属性

例外

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

注解

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

另请参阅

适用于

DataTable(String, String)

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

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

public:
 DataTable(System::String ^ tableName, System::String ^ tableNamespace);
public DataTable (string? tableName, string? tableNamespace);
public DataTable (string tableName, string tableNamespace);
new System.Data.DataTable : string * string -> System.Data.DataTable
Public Sub New (tableName As String, tableNamespace As String)

参数

tableName
String

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

tableNamespace
String

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

另请参阅

适用于