DataTable コンストラクター

定義

DataTable クラスの新しいインスタンスを初期化します。

オーバーロード

名前 説明
DataTable()

引数を指定せず、 DataTable クラスの新しいインスタンスを初期化します。

DataTable(String)

指定したテーブル名を使用して、 DataTable クラスの新しいインスタンスを初期化します。

DataTable(SerializationInfo, StreamingContext)

シリアル化されたデータを使用して、 DataTable クラスの新しいインスタンスを初期化します。

DataTable(String, String)

指定したテーブル名と名前空間を使用して、 DataTable クラスの新しいインスタンスを初期化します。

DataTable()

引数を指定せず、 DataTable クラスの新しいインスタンスを初期化します。

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

次の例では、DataTableDataColumnを使用して新しい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 空の文字列 ("")
ロケール DataSet オブジェクトの CultureInfo ( Locale プロパティによって返されます) と同じです。親が存在しない場合、既定値は現在のシステム CultureInfoです。
MinimumCapacity 50 行。

これらのプロパティの値は、プロパティを個別に呼び出して変更できます。

こちらもご覧ください

適用対象

DataTable(String)

指定したテーブル名を使用して、 DataTable クラスの新しいインスタンスを初期化します。

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

テーブルに付ける名前。 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 クラスの新しいインスタンスを初期化します。

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

データ セットのシリアル化されたデータ。

context
StreamingContext

シリアル化されたストリームに関するコンテキスト情報。

例外

.NET 7 以降のバージョンのみ: info にはバイナリ データが含まれています。

注釈

DataTableには、ISerializable コンストラクターのこの実装が必要です。

こちらもご覧ください

適用対象

DataTable(String, String)

指定したテーブル名と名前空間を使用して、 DataTable クラスの新しいインスタンスを初期化します。

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

テーブルに付ける名前。 tableNamenullまたは空の文字列の場合、DataTableCollectionに追加されたときに既定の名前が指定されます。

tableNamespace
String

DataTableに格納されているデータの XML 表現の名前空間。

こちらもご覧ください

適用対象