DataTable 建構函式
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
初始化 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 ()
範例
下列範例會使用 和 DataRow 建立新的 DataTableDataColumn ,並在 控制項中 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 回的父物件 CultureInfo (相同;如果沒有父 DataSet 代,則預設值為目前的系統 CultureInfo 。 |
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
要提供資料表的名稱。 如果 tableName
是 null
或空字串,則會在加入至 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
要提供資料表的名稱。 如果 tableName
是 null
或空字串,則會在加入至 DataTableCollection 時提供預設名稱。
- tableNamespace
- String
儲存於 DataTable
中資料之 XML 表示的命名空間。
另請參閱
適用於
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應