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
- Source:
- DataTable.cs
- Source:
- DataTable.cs
初始化没有参数的 DataTable 类的新实例。
public:
DataTable();
public DataTable();
Public Sub New ()
示例
以下示例创建一个新 DataTable 项 DataColumn ,并在 DataRow控件中 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对象的 (属性Locale返回)相同;如果不存在父对象CultureInfo,则默认值为当前系统CultureInfo。 |
| MinimumCapacity | 50 行。 |
可以通过对属性的单独调用来更改这些属性的值。
另请参阅
适用于
DataTable(String)
- Source:
- DataTable.cs
- Source:
- DataTable.cs
- 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
要为表指定的名称。 如果
示例
以下示例创建一个 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
- 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);
[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)
- Source:
- DataTable.cs
- Source:
- DataTable.cs
- 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
要为表指定的名称。 如果
- tableNamespace
- String
存储在 .. 中的数据 DataTable的 XML 表示形式的命名空间。