DataTable 构造函数

定义

初始化 DataTable 类的新实例。Initializes a new instance of the DataTable class.

重载

DataTable()

在不使用参数的情况下初始化 DataTable 类的新实例。Initializes a new instance of the DataTable class with no arguments.

DataTable(String)

使用指定的表名初始化 DataTable 类的新实例。Initializes a new instance of the DataTable class with the specified table name.

DataTable(SerializationInfo, StreamingContext)

使用 SerializationInfoStreamingContext 初始化 DataTable 类的新实例。Initializes a new instance of the DataTable class with the SerializationInfo and the StreamingContext.

DataTable(String, String)

使用指定的表名和命名空间初始化 DataTable 类的新实例。Initializes a new instance of the DataTable class using the specified table name and namespace.

DataTable()

在不使用参数的情况下初始化 DataTable 类的新实例。Initializes a new instance of the DataTable class with no arguments.

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

示例

下面的示例使用和创建一个新的 DataTable DataColumn DataRow ,并在控件中显示它 DataGridViewThe following example creates a new DataTable with DataColumn and DataRow, and displays it in a DataGridView control.

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

注解

构造函数设置对象的所有属性的初始值 DataTableThe constructor sets initial values for all properties of the DataTable object. 下表显示了属性及其默认值。The following table shows the properties and their default values. 创建实例时 DataTable ,以下读/写属性将设置为初始值。When an instance of DataTable is created, the following read/write properties are set to initial values.

propertiesProperty 默认值Default value
CaseSensitiveCaseSensitive 如果它属于父级,则与父级相同 DataSetSame as the parent DataSet, if it belongs to one. 否则为 falseOtherwise, false.
DisplayExpressionDisplayExpression 空字符串 ( "" ) Empty string ("")
区域设置Locale DataSet CultureInfo) 属性返回的父对象 (相同 Locale ; 如果不存在父对象,则默认值为当前系统 CultureInfoSame as the parent DataSet object's CultureInfo (returned by the Locale property); if no parent exists, the default is the current system CultureInfo.
MinimumCapacityMinimumCapacity 50行。50 rows.

可以通过单独调用属性来更改任何这些属性的值。You can change the value for any of these properties through a separate call to the property.

适用于

DataTable(String)

使用指定的表名初始化 DataTable 类的新实例。Initializes a new instance of the DataTable class with the specified table name.

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

要向表提供的名称。The name to give the table. 如果 tableNamenull 或是空字符串,则在添加到 DataTableCollection 中时指定默认名称。If tableName is null or an empty string, a default name is given when added to the DataTableCollection.

示例

下面的示例创建一个 DataTable ,并在控件中显示它 DataGridViewThe following example creates a DataTable and displays it in a DataGridView control.

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)

使用 SerializationInfoStreamingContext 初始化 DataTable 类的新实例。Initializes a new instance of the DataTable class with the SerializationInfo and the StreamingContext.

protected:
 DataTable(System::Runtime::Serialization::SerializationInfo ^ info, System::Runtime::Serialization::StreamingContext context);
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
Protected Sub New (info As SerializationInfo, context As StreamingContext)

参数

info
SerializationInfo

序列化或反序列化对象所需的数据。The data needed to serialize or deserialize an object.

context
StreamingContext

给定序列化流的源和目标。The source and destination of a given serialized stream.

注解

DataTable 构造函数的实现是必需的 ISerializableThis implementation of the DataTable constructor is required for ISerializable.

适用于

DataTable(String, String)

使用指定的表名和命名空间初始化 DataTable 类的新实例。Initializes a new instance of the DataTable class using the specified table name and namespace.

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

要向表提供的名称。The name to give the table. 如果 tableNamenull 或是空字符串,则在添加到 DataTableCollection 中时指定默认名称。If tableName is null or an empty string, a default name is given when added to the DataTableCollection.

tableNamespace
String

DataTable 中所存储数据的 XML 表示形式的命名空间。The namespace for the XML representation of the data stored in the DataTable.

适用于