DataTable DataTable DataTable DataTable Constructors

定義

多載

DataTable() DataTable() DataTable()

不使用引數來初始化 DataTable 類別的新執行個體。Initializes a new instance of the DataTable class with no arguments.

DataTable(String) DataTable(String) DataTable(String) DataTable(String)

使用指定的資料表名稱,初始化 DataTable 類別的新執行個體。Initializes a new instance of the DataTable class with the specified table name.

DataTable(SerializationInfo, StreamingContext) DataTable(SerializationInfo, StreamingContext) DataTable(SerializationInfo, StreamingContext) DataTable(SerializationInfo, StreamingContext)

使用 DataTableSerializationInfo,初始化 StreamingContext 類別的新執行個體。Initializes a new instance of the DataTable class with the SerializationInfo and the StreamingContext.

DataTable(String, String) DataTable(String, String) DataTable(String, String) DataTable(String, String)

使用指定的資料表名稱和命名空間,初始化 DataTable 類別的新執行個體。Initializes a new instance of the DataTable class using the specified table name and namespace.

DataTable() DataTable() DataTable()

不使用引數來初始化 DataTable 類別的新執行個體。Initializes a new instance of the DataTable class with no arguments.

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

範例

DataTable下列範例會使用DataColumnDataRow建立新的,並將它顯示在DataGridView控制項中。The 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

備註

此函式會為DataTable物件的所有屬性設定初始值。The 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.

屬性Property 預設值Default value
CaseSensitiveCaseSensitive 與父系相同( DataSet如果它屬於一個父代)。Same 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(String) DataTable(String) 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);
new System.Data.DataTable : string -> System.Data.DataTable
Public Sub New (tableName As String)

參數

tableName
String String String 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 ,並將它顯示DataGridView在控制項中。The 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 DataTable = 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(SerializationInfo, StreamingContext) DataTable(SerializationInfo, StreamingContext) DataTable(SerializationInfo, StreamingContext)

使用 DataTableSerializationInfo,初始化 StreamingContext 類別的新執行個體。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 SerializationInfo SerializationInfo SerializationInfo

序列化或還原序列化物件所需的資料。The data needed to serialize or deserialize an object.

context
StreamingContext StreamingContext StreamingContext 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(String, String) DataTable(String, String) 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);
new System.Data.DataTable : string * string -> System.Data.DataTable
Public Sub New (tableName As String, tableNamespace As String)

參數

tableName
String String String 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 String String String

儲存於 DataTable 中資料之 XML 表示的命名空間。The namespace for the XML representation of the data stored in the DataTable.

另請參閱

適用於