共用方式為


DataTable 建構函式

定義

初始化 DataTable 類別的新執行個體。

多載

名稱 Description
DataTable()

初始化一個無參數的新類別實例 DataTable

DataTable(String)

初始化一個以指定資料表名稱的新 DataTable 類別實例。

DataTable(SerializationInfo, StreamingContext)
已淘汰.

初始化一個新的類別實例 DataTable ,並使用序列化資料。

DataTable(String, String)

使用指定的資料表名稱與命名空間初始化該類別的新實例 DataTable

DataTable()

來源:
DataTable.cs
來源:
DataTable.cs
來源:
DataTable.cs
來源:
DataTable.cs
來源:
DataTable.cs

初始化一個無參數的新類別實例 DataTable

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

範例

以下範例將建立 DataTable 一個新節點,且 為 DataColumnDataRow,並在控制 DataGridView 項中顯示。

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 實例時,以下的讀寫屬性會被設定為初始值。

房產 預設值
CaseSensitive 如果屬於某個家長 DataSet,這也是同樣的。 否則為 false
DisplayExpression 空字串(“”)
Locale 與父 DataSet 物件 CultureInfo 的物件相同(由 Locale 屬性回傳);若不存在父物件,預設為目前系統 CultureInfo
最小容量 50排。

你可以透過單獨呼叫這些屬性來更改這些屬性的值。

另請參閱

適用於

DataTable(String)

來源:
DataTable.cs
來源:
DataTable.cs
來源:
DataTable.cs
來源:
DataTable.cs
來源:
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)

來源:
DataTable.cs
來源:
DataTable.cs
來源:
DataTable.cs
來源:
DataTable.cs
來源:
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);
[System.Diagnostics.CodeAnalysis.RequiresDynamicCode("Members from serialized types may use dynamic code generation.")]
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("Members from serialized types may be trimmed if not referenced directly.")]
[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);
protected DataTable(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context);
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("Members from serialized types may be trimmed if not referenced directly.")]
protected DataTable(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context);
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("Members from serialized types may be trimmed if not referenced directly.")]
[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);
[<System.Diagnostics.CodeAnalysis.RequiresDynamicCode("Members from serialized types may use dynamic code generation.")>]
[<System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("Members from serialized types may be trimmed if not referenced directly.")>]
[<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
new System.Data.DataTable : System.Runtime.Serialization.SerializationInfo * System.Runtime.Serialization.StreamingContext -> System.Data.DataTable
[<System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("Members from serialized types may be trimmed if not referenced directly.")>]
new System.Data.DataTable : System.Runtime.Serialization.SerializationInfo * System.Runtime.Serialization.StreamingContext -> System.Data.DataTable
[<System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("Members from serialized types may be trimmed if not referenced directly.")>]
[<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)

來源:
DataTable.cs
來源:
DataTable.cs
來源:
DataTable.cs
來源:
DataTable.cs
來源:
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.

另請參閱

適用於