次の方法で共有


DataView.ToTable メソッド

定義

既存のDataView の行に基づく新しい DataTable を作成して返します。

オーバーロード

ToTable(Boolean, String[])

既存のDataView の行に基づく新しい DataTable を作成して返します。

ToTable(String)

既存のDataView の行に基づく新しい DataTable を作成して返します。

ToTable(String, Boolean, String[])

既存のDataView の行に基づく新しい DataTable を作成して返します。

ToTable()

既存のDataView の行に基づく新しい DataTable を作成して返します。

注釈

返された DataTable は、ソース DataTableの次のプロパティの値を保持します: NamespacePrefixLocale、および CaseSensitive。 結果 DataTable の の各列には、基になる DataTableのすべての対応するプロパティの同じコピーが含まれています。

DataRow返される DataTable のインスタンスには、CLR セマンティクスと一致する値が含まれます。 つまり、ユーザー定義型以外のすべてのデータ型に対して、対応する列値が値によってコピーされます。 ユーザー定義データ型の場合、列データは参照によってコピーされます。

ToTable(Boolean, String[])

ソース:
DataView.cs
ソース:
DataView.cs
ソース:
DataView.cs

既存のDataView の行に基づく新しい DataTable を作成して返します。

public:
 System::Data::DataTable ^ ToTable(bool distinct, ... cli::array <System::String ^> ^ columnNames);
public System.Data.DataTable ToTable (bool distinct, params string[] columnNames);
member this.ToTable : bool * string[] -> System.Data.DataTable
Public Function ToTable (distinct As Boolean, ParamArray columnNames As String()) As DataTable

パラメーター

distinct
Boolean

true の場合、返された DataTable にはすべての列に対して重複しない値を持つ行が含まれています。 既定値は false です。

columnNames
String[]

返された DataTable に含まれる列名の一覧を格納する文字列の配列。 DataTable には、この配列内に表示される順序で、指定された列が含まれています。

戻り値

要求した行と列を格納する新しい DataTable インスタンス。

次のコンソール アプリケーションの例を作成します、 DataTable、入力、 DataTable 、データを並べ替えます、 DataView、し、最後に作成、 DataTable 2 つの列を持つすべての値が一意の行に制限されます。

private static void DemonstrateDataView()
{
    // Create a DataTable with three columns.
    DataTable table = new DataTable("NewTable");
    Console.WriteLine("Original table name: " + table.TableName);
    DataColumn column = new DataColumn("ID", typeof(System.Int32));
    table.Columns.Add(column);

    column = new DataColumn("Category", typeof(System.String));
    table.Columns.Add(column);

    column = new DataColumn("Product", typeof(System.String));
    table.Columns.Add(column);

    column = new DataColumn("QuantityInStock", typeof(System.Int32));
    table.Columns.Add(column);

    // Add some items.
    DataRow row = table.NewRow();
    row.ItemArray = new object[] { 1, "Fruit", "Apple", 14 };
    table.Rows.Add(row);

    row = table.NewRow();
    row.ItemArray = new object[] { 2, "Fruit", "Orange", 27 };
    table.Rows.Add(row);

    row = table.NewRow();
    row.ItemArray = new object[] { 3, "Bread", "Muffin", 23 };
    table.Rows.Add(row);

    row = table.NewRow();
    row.ItemArray = new object[] { 4, "Fish", "Salmon", 12 };
    table.Rows.Add(row);

    row = table.NewRow();
    row.ItemArray = new object[] { 5, "Fish", "Salmon", 15 };
    table.Rows.Add(row);

    row = table.NewRow();
    row.ItemArray = new object[] { 6, "Bread", "Croissant", 23};
    table.Rows.Add(row);

    // Mark all rows as "accepted". Not required
    // for this particular example.
    table.AcceptChanges();

    // Print current table values.
    PrintTableOrView(table, "Current Values in Table");

    DataView view = new DataView(table);
    view.Sort = "Category";
    PrintTableOrView(view, "Current Values in View");

    DataTable newTable = view.ToTable(true, "Category", "QuantityInStock");
    PrintTableOrView(newTable, "Table created from sorted DataView");
    Console.WriteLine("New table name: " + newTable.TableName);

    Console.WriteLine("Press any key to continue.");
    Console.ReadKey();
}

private static void PrintTableOrView(DataView dv, string label)
{
    System.IO.StringWriter sw;
    string output;
    DataTable table = dv.Table;

    Console.WriteLine(label);

    // Loop through each row in the view.
    foreach (DataRowView rowView in dv)
    {
        sw = new System.IO.StringWriter();

        // Loop through each column.
        foreach (DataColumn col in table.Columns)
        {
            // Output the value of each column's data.
            sw.Write(rowView[col.ColumnName].ToString() + ", ");
        }
        output = sw.ToString();
        // Trim off the trailing ", ", so the output looks correct.
        if (output.Length > 2)
        {
            output = output.Substring(0, output.Length - 2);
        }
        // Display the row in the console window.
        Console.WriteLine(output);
    }
    Console.WriteLine();
}

private static void PrintTableOrView(DataTable table, string label)
{
    System.IO.StringWriter sw;
    string output;

    Console.WriteLine(label);

    // Loop through each row in the table.
    foreach (DataRow row in table.Rows)
    {
        sw = new System.IO.StringWriter();
        // Loop through each column.
        foreach (DataColumn col in table.Columns)
        {
            // Output the value of each column's data.
            sw.Write(row[col].ToString() + ", ");
        }
        output = sw.ToString();
        // Trim off the trailing ", ", so the output looks correct.
        if (output.Length > 2)
        {
            output = output.Substring(0, output.Length - 2);
        }
        // Display the row in the console window.
        Console.WriteLine(output);
    }
    Console.WriteLine();
}
Private Sub DemonstrateDataView()
    ' Create a DataTable with three columns.
    Dim table As New DataTable("NewTable")
    Console.WriteLine("Original table name: " & table.TableName)
    Dim column As New DataColumn("ID", GetType(System.Int32))
    table.Columns.Add(column)

    column = New DataColumn("Category", GetType(System.String))
    table.Columns.Add(column)

    column = New DataColumn("Product", GetType(System.String))
    table.Columns.Add(column)

    column = New DataColumn("QuantityInStock", GetType(System.Int32))
    table.Columns.Add(column)


    ' Add some items.
    Dim row As DataRow = table.NewRow()
    row.ItemArray = New Object() {1, "Fruit", "Apple", 14}
    table.Rows.Add(row)

    row = table.NewRow()
    row.ItemArray = New Object() {2, "Fruit", "Orange", 27}
    table.Rows.Add(row)

    row = table.NewRow()
    row.ItemArray = New Object() {3, "Bread", "Muffin", 23}
    table.Rows.Add(row)

    row = table.NewRow()
    row.ItemArray = New Object() {4, "Fish", "Salmon", 12}
    table.Rows.Add(row)

    row = table.NewRow()
    row.ItemArray = New Object() {5, "Fish", "Salmon", 15}
    table.Rows.Add(row)

    row = table.NewRow()
    row.ItemArray = New Object() {6, "Bread", "Croissant", 23}
    table.Rows.Add(row)

    ' Mark all rows as "accepted". Not required
    ' for this particular example.
    table.AcceptChanges()

    ' Print current table values.
    PrintTableOrView(table, "Current Values in Table")

    Dim view As New DataView(table)
    view.Sort = "Category"
    PrintTableOrView(view, "Current Values in View")

    Dim newTable As DataTable = view.ToTable( _
        True, "Category", "QuantityInStock")
    PrintTableOrView(newTable, "Table created from sorted DataView")
    Console.WriteLine("New table name: " & newTable.TableName)

    Console.WriteLine("Press any key to continue.")
    Console.ReadKey()
End Sub

Private Sub PrintTableOrView(ByVal dv As DataView, ByVal label As String)
    Dim sw As System.IO.StringWriter
    Dim output As String
    Dim table As DataTable = dv.Table

    Console.WriteLine(label)

    ' Loop through each row in the view.
    For Each rowView As DataRowView In dv
        sw = New System.IO.StringWriter

        ' Loop through each column.
        For Each col As DataColumn In table.Columns
            ' Output the value of each column's data.
            sw.Write(rowView(col.ColumnName).ToString() & ", ")
        Next
        output = sw.ToString
        ' Trim off the trailing ", ", so the output looks correct.
        If output.Length > 2 Then
            output = output.Substring(0, output.Length - 2)
        End If
        ' Display the row in the console window.
        Console.WriteLine(output)
    Next
    Console.WriteLine()
End Sub

Private Sub PrintTableOrView(ByVal table As DataTable, ByVal label As String)
    Dim sw As System.IO.StringWriter
    Dim output As String

    Console.WriteLine(label)

    ' Loop through each row in the table.
    For Each row As DataRow In table.Rows
        sw = New System.IO.StringWriter
        ' Loop through each column.
        For Each col As DataColumn In table.Columns
            ' Output the value of each column's data.
            sw.Write(row(col).ToString() & ", ")
        Next
        output = sw.ToString
        ' Trim off the trailing ", ", so the output looks correct.
        If output.Length > 2 Then
            output = output.Substring(0, output.Length - 2)
        End If
        ' Display the row in the console window.
        Console.WriteLine(output)
    Next
    Console.WriteLine()
End Sub

この例では、次の出力がコンソール ウィンドウに表示されます。

Original table name: NewTable  
Current Values in Table  
1, Fruit, Apple, 14  
2, Fruit, Orange, 27  
3, Bread, Muffin, 23  
4, Fish, Salmon, 12  
5, Fish, Salmon, 15  
6, Bread, Croissant, 23  

Current Values in View  
3, Bread, Muffin, 23  
6, Bread, Croissant, 23  
4, Fish, Salmon, 12  
5, Fish, Salmon, 15  
1, Fruit, Apple, 14  
2, Fruit, Orange, 27  

Table created from sorted DataView  
Bread, 23  
Fish, 12  
Fish, 15  
Fruit, 14  
Fruit, 27  

New table name: NewTable  

注釈

このメソッドでは出力 DataTableの名前を指定できないため、その名前はソース DataTableの名前と同じです。

適用対象

ToTable(String)

ソース:
DataView.cs
ソース:
DataView.cs
ソース:
DataView.cs

既存のDataView の行に基づく新しい DataTable を作成して返します。

public:
 System::Data::DataTable ^ ToTable(System::String ^ tableName);
public System.Data.DataTable ToTable (string? tableName);
public System.Data.DataTable ToTable (string tableName);
member this.ToTable : string -> System.Data.DataTable
Public Function ToTable (tableName As String) As DataTable

パラメーター

tableName
String

返された DataTable の名前。

戻り値

要求した行と列を格納する新しい DataTable インスタンス。

次のコンソール アプリケーションの例では、 を DataTable作成し、 に DataTable データを入力し、元のデータに基づいてフィルター処理されたビューを作成し、最後にフィルター処理された行を含む新しい名前で を作成 DataTable します。

private static void DemonstrateDataView()
{
    // Create a DataTable with three columns.
    DataTable table = new DataTable("NewTable");
    Console.WriteLine("Original table name: " + table.TableName);
    DataColumn column = new DataColumn("ID", typeof(System.Int32));
    table.Columns.Add(column);

    column = new DataColumn("Category", typeof(System.String));
    table.Columns.Add(column);

    column = new DataColumn("Product", typeof(System.String));
    table.Columns.Add(column);

    column = new DataColumn("QuantityInStock", typeof(System.Int32));
    table.Columns.Add(column);

    // Add some items.
    DataRow row = table.NewRow();
    row.ItemArray = new object[] { 1, "Fruit", "Apple", 14 };
    table.Rows.Add(row);

    row = table.NewRow();
    row.ItemArray = new object[] { 2, "Fruit", "Orange", 27 };
    table.Rows.Add(row);

    row = table.NewRow();
    row.ItemArray = new object[] { 3, "Bread", "Muffin", 23 };
    table.Rows.Add(row);

    row = table.NewRow();
    row.ItemArray = new object[] { 4, "Fish", "Salmon", 12 };
    table.Rows.Add(row);

    // Mark all rows as "accepted". Not really required
    // for this particular example.
    table.AcceptChanges();

    // Print current table values.
    PrintTableOrView(table, "Current Values in Table");

    DataView view = new DataView(table);
    view.RowFilter = "QuantityInStock > 15";
    PrintTableOrView(view, "Current Values in View");

    DataTable newTable = view.ToTable("FilteredTable");
    PrintTableOrView(newTable, "Table created from filtered DataView");
    Console.WriteLine("New table name: " + newTable.TableName);

    Console.WriteLine("Press any key to continue.");
    Console.ReadKey();
}

private static void PrintTableOrView(DataView dv, string label)
{
    System.IO.StringWriter sw;
    string output;
    DataTable table = dv.Table;

    Console.WriteLine(label);

    // Loop through each row in the view.
    foreach (DataRowView rowView in dv)
    {
        sw = new System.IO.StringWriter();

        // Loop through each column.
        foreach (DataColumn col in table.Columns)
        {
            // Output the value of each column's data.
            sw.Write(rowView[col.ColumnName].ToString() + ", ");
        }
        output = sw.ToString();
        // Trim off the trailing ", ", so the output looks correct.
        if (output.Length > 2)
        {
            output = output.Substring(0, output.Length - 2);
        }
        // Display the row in the console window.
        Console.WriteLine(output);
    }
    Console.WriteLine();
}

private static void PrintTableOrView(DataTable table, string label)
{
    System.IO.StringWriter sw;
    string output;

    Console.WriteLine(label);

    // Loop through each row in the table.
    foreach (DataRow row in table.Rows)
    {
        sw = new System.IO.StringWriter();
        // Loop through each column.
        foreach (DataColumn col in table.Columns)
        {
            // Output the value of each column's data.
            sw.Write(row[col].ToString() + ", ");
        }
        output = sw.ToString();
        // Trim off the trailing ", ", so the output looks correct.
        if (output.Length > 2)
        {
            output = output.Substring(0, output.Length - 2);
        }
        // Display the row in the console window.
        Console.WriteLine(output);
    }
    Console.WriteLine();
}
Private Sub DemonstrateDataView()
    ' Create a DataTable with three columns.
    Dim table As New DataTable("NewTable")
    Console.WriteLine("Original table name: " & table.TableName)
    Dim column As New DataColumn("ID", GetType(System.Int32))
    table.Columns.Add(column)

    column = New DataColumn("Category", GetType(System.String))
    table.Columns.Add(column)

    column = New DataColumn("Product", GetType(System.String))
    table.Columns.Add(column)

    column = New DataColumn("QuantityInStock", GetType(System.Int32))
    table.Columns.Add(column)


    ' Add some items.
    Dim row As DataRow = table.NewRow()
    row.ItemArray = New Object() {1, "Fruit", "Apple", 14}
    table.Rows.Add(row)

    row = table.NewRow()
    row.ItemArray = New Object() {2, "Fruit", "Orange", 27}
    table.Rows.Add(row)

    row = table.NewRow()
    row.ItemArray = New Object() {3, "Bread", "Muffin", 23}
    table.Rows.Add(row)

    row = table.NewRow()
    row.ItemArray = New Object() {4, "Fish", "Salmon", 12}
    table.Rows.Add(row)

    ' Mark all rows as "accepted". Not required
    ' for this particular example.
    table.AcceptChanges()

    ' Print current table values.
    PrintTableOrView(table, "Current Values in Table")

    Dim view As New DataView(table)
    view.RowFilter = "QuantityInStock > 15"
    PrintTableOrView(view, "Current Values in View")

    Dim newTable As DataTable = view.ToTable("FilteredTable")
    PrintTableOrView(newTable, "Table created from filtered DataView")
    Console.WriteLine("New table name: " & newTable.TableName)

    Console.WriteLine("Press any key to continue.")
    Console.ReadKey()
End Sub

Private Sub PrintTableOrView(ByVal dv As DataView, ByVal label As String)
    Dim sw As System.IO.StringWriter
    Dim output As String
    Dim table As DataTable = dv.Table

    Console.WriteLine(label)

    ' Loop through each row in the view.
    For Each rowView As DataRowView In dv
        sw = New System.IO.StringWriter

        ' Loop through each column.
        For Each col As DataColumn In table.Columns
            ' Output the value of each column's data.
            sw.Write(rowView(col.ColumnName).ToString() & ", ")
        Next
        output = sw.ToString
        ' Trim off the trailing ", ", so the output looks correct.
        If output.Length > 2 Then
            output = output.Substring(0, output.Length - 2)
        End If
        ' Display the row in the console window.
        Console.WriteLine(output)
    Next
    Console.WriteLine()
End Sub

Private Sub PrintTableOrView(ByVal table As DataTable, ByVal label As String)
    Dim sw As System.IO.StringWriter
    Dim output As String

    Console.WriteLine(label)

    ' Loop through each row in the table.
    For Each row As DataRow In table.Rows
        sw = New System.IO.StringWriter
        ' Loop through each column.
        For Each col As DataColumn In table.Columns
            ' Output the value of each column's data.
            sw.Write(row(col).ToString() & ", ")
        Next
        output = sw.ToString
        ' Trim off the trailing ", ", so the output looks correct.
        If output.Length > 2 Then
            output = output.Substring(0, output.Length - 2)
        End If
        ' Display the row in the console window.
        Console.WriteLine(output)
    Next
    Console.WriteLine()
End Sub

この例では、次のテキストがコンソール ウィンドウに表示されます。

Original table name: NewTable  
Current Values in Table  
1, Fruit, Apple, 14  
2, Fruit, Orange, 27  
3, Bread, Muffin, 23  
4, Fish, Salmon, 12  

Current Values in View  
2, Fruit, Orange, 27  
3, Bread, Muffin, 23  

Table created from filtered DataView  
2, Fruit, Orange, 27  
3, Bread, Muffin, 23  

New table name: FilteredTable  

注釈

このメソッドでは使用可能な列のサブセットを指定できないため、出力テーブルには入力テーブルと同じ列が含まれます。

こちらもご覧ください

適用対象

ToTable(String, Boolean, String[])

ソース:
DataView.cs
ソース:
DataView.cs
ソース:
DataView.cs

既存のDataView の行に基づく新しい DataTable を作成して返します。

public:
 System::Data::DataTable ^ ToTable(System::String ^ tableName, bool distinct, ... cli::array <System::String ^> ^ columnNames);
public System.Data.DataTable ToTable (string? tableName, bool distinct, params string[] columnNames);
public System.Data.DataTable ToTable (string tableName, bool distinct, params string[] columnNames);
member this.ToTable : string * bool * string[] -> System.Data.DataTable
Public Function ToTable (tableName As String, distinct As Boolean, ParamArray columnNames As String()) As DataTable

パラメーター

tableName
String

返された DataTable の名前。

distinct
Boolean

true の場合、返された DataTable にはすべての列に対して重複しない値を持つ行が含まれています。 既定値は false です。

columnNames
String[]

返された DataTable に含まれる列名の一覧を格納する文字列の配列。 DataTable には、この配列内に表示される順序で、指定された列が含まれています。

戻り値

要求した行と列を格納する新しい DataTable インスタンス。

次のコンソール アプリケーションの例を作成します、 DataTable、入力、 DataTable 、データを並べ替えます、 DataView、し、最後に作成、DataTableだけ 2 つの列を含む新しい名前を持つすべての値が一意の行に制限されます。

private static void DemonstrateDataView()
{
    // Create a DataTable with three columns.
    DataTable table = new DataTable("NewTable");
    Console.WriteLine("Original table name: " + table.TableName);
    DataColumn column = new DataColumn("ID", typeof(System.Int32));
    table.Columns.Add(column);

    column = new DataColumn("Category", typeof(System.String));
    table.Columns.Add(column);

    column = new DataColumn("Product", typeof(System.String));
    table.Columns.Add(column);

    column = new DataColumn("QuantityInStock", typeof(System.Int32));
    table.Columns.Add(column);

    // Add some items.
    DataRow row = table.NewRow();
    row.ItemArray = new object[] { 1, "Fruit", "Apple", 14 };
    table.Rows.Add(row);

    row = table.NewRow();
    row.ItemArray = new object[] { 2, "Fruit", "Orange", 27 };
    table.Rows.Add(row);

    row = table.NewRow();
    row.ItemArray = new object[] { 3, "Bread", "Muffin", 23 };
    table.Rows.Add(row);

    row = table.NewRow();
    row.ItemArray = new object[] { 4, "Fish", "Salmon", 12 };
    table.Rows.Add(row);

    row = table.NewRow();
    row.ItemArray = new object[] { 5, "Fish", "Salmon", 15 };
    table.Rows.Add(row);

    row = table.NewRow();
    row.ItemArray = new object[] { 6, "Bread", "Croissant", 23};
    table.Rows.Add(row);

    // Mark all rows as "accepted". Not required
    // for this particular example.
    table.AcceptChanges();

    // Print current table values.
    PrintTableOrView(table, "Current Values in Table");

    DataView view = new DataView(table);
    view.Sort = "Category";
    PrintTableOrView(view, "Current Values in View");

    DataTable newTable = view.ToTable("UniqueData", true,
        "Category", "QuantityInStock");
    PrintTableOrView(newTable, "Table created from sorted DataView");
    Console.WriteLine("New table name: " + newTable.TableName);

    Console.WriteLine("Press any key to continue.");
    Console.ReadKey();
}

private static void PrintTableOrView(DataView dv, string label)
{
    System.IO.StringWriter sw;
    string output;
    DataTable table = dv.Table;

    Console.WriteLine(label);

    // Loop through each row in the view.
    foreach (DataRowView rowView in dv)
    {
        sw = new System.IO.StringWriter();

        // Loop through each column.
        foreach (DataColumn col in table.Columns)
        {
            // Output the value of each column's data.
            sw.Write(rowView[col.ColumnName].ToString() + ", ");
        }
        output = sw.ToString();
        // Trim off the trailing ", ", so the output looks correct.
        if (output.Length > 2)
        {
            output = output.Substring(0, output.Length - 2);
        }
        // Display the row in the console window.
        Console.WriteLine(output);
    }
    Console.WriteLine();
}

private static void PrintTableOrView(DataTable table, string label)
{
    System.IO.StringWriter sw;
    string output;

    Console.WriteLine(label);

    // Loop through each row in the table.
    foreach (DataRow row in table.Rows)
    {
        sw = new System.IO.StringWriter();
        // Loop through each column.
        foreach (DataColumn col in table.Columns)
        {
            // Output the value of each column's data.
            sw.Write(row[col].ToString() + ", ");
        }
        output = sw.ToString();
        // Trim off the trailing ", ", so the output looks correct.
        if (output.Length > 2)
        {
            output = output.Substring(0, output.Length - 2);
        }
        // Display the row in the console window.
        Console.WriteLine(output);
    } //
    Console.WriteLine();
}
Private Sub DemonstrateDataView()
    ' Create a DataTable with three columns.
    Dim table As New DataTable("NewTable")
    Console.WriteLine("Original table name: " & table.TableName)
    Dim column As New DataColumn("ID", GetType(System.Int32))
    table.Columns.Add(column)

    column = New DataColumn("Category", GetType(System.String))
    table.Columns.Add(column)

    column = New DataColumn("Product", GetType(System.String))
    table.Columns.Add(column)

    column = New DataColumn("QuantityInStock", GetType(System.Int32))
    table.Columns.Add(column)


    ' Add some items.
    Dim row As DataRow = table.NewRow()
    row.ItemArray = New Object() {1, "Fruit", "Apple", 14}
    table.Rows.Add(row)

    row = table.NewRow()
    row.ItemArray = New Object() {2, "Fruit", "Orange", 27}
    table.Rows.Add(row)

    row = table.NewRow()
    row.ItemArray = New Object() {3, "Bread", "Muffin", 23}
    table.Rows.Add(row)

    row = table.NewRow()
    row.ItemArray = New Object() {4, "Fish", "Salmon", 12}
    table.Rows.Add(row)

    row = table.NewRow()
    row.ItemArray = New Object() {5, "Fish", "Salmon", 15}
    table.Rows.Add(row)

    row = table.NewRow()
    row.ItemArray = New Object() {6, "Bread", "Croissant", 23}
    table.Rows.Add(row)

    ' Mark all rows as "accepted". Not required
    ' for this particular example.
    table.AcceptChanges()

    ' Print current table values.
    PrintTableOrView(table, "Current Values in Table")

    Dim view As New DataView(table)
    view.Sort = "Category"
    PrintTableOrView(view, "Current Values in View")

    Dim newTable As DataTable = view.ToTable("UniqueData", _
        True, "Category", "QuantityInStock")
    PrintTableOrView(newTable, "Table created from sorted DataView")
    Console.WriteLine("New table name: " & newTable.TableName)

    Console.WriteLine("Press any key to continue.")
    Console.ReadKey()
End Sub

Private Sub PrintTableOrView(ByVal dv As DataView, ByVal label As String)
    Dim sw As System.IO.StringWriter
    Dim output As String
    Dim table As DataTable = dv.Table

    Console.WriteLine(label)

    ' Loop through each row in the view.
    For Each rowView As DataRowView In dv
        sw = New System.IO.StringWriter

        ' Loop through each column.
        For Each col As DataColumn In table.Columns
            ' Output the value of each column's data.
            sw.Write(rowView(col.ColumnName).ToString() & ", ")
        Next
        output = sw.ToString
        ' Trim off the trailing ", ", so the output looks correct.
        If output.Length > 2 Then
            output = output.Substring(0, output.Length - 2)
        End If
        ' Display the row in the console window.
        Console.WriteLine(output)
    Next
    Console.WriteLine()
End Sub

Private Sub PrintTableOrView(ByVal table As DataTable, ByVal label As String)
    Dim sw As System.IO.StringWriter
    Dim output As String

    Console.WriteLine(label)

    ' Loop through each row in the table.
    For Each row As DataRow In table.Rows
        sw = New System.IO.StringWriter
        ' Loop through each column.
        For Each col As DataColumn In table.Columns
            ' Output the value of each column's data.
            sw.Write(row(col).ToString() & ", ")
        Next
        output = sw.ToString
        ' Trim off the trailing ", ", so the output looks correct.
        If output.Length > 2 Then
            output = output.Substring(0, output.Length - 2)
        End If
        ' Display the row in the console window.
        Console.WriteLine(output)
    Next
    Console.WriteLine()
End Sub

この例では、次の出力がコンソール ウィンドウに表示されます。

Original table name: NewTable  
Current Values in Table  
1, Fruit, Apple, 14  
2, Fruit, Orange, 27  
3, Bread, Muffin, 23  
4, Fish, Salmon, 12  
5, Fish, Salmon, 15  
6, Bread, Croissant, 23  

Current Values in View  
3, Bread, Muffin, 23  
6, Bread, Croissant, 23  
4, Fish, Salmon, 12  
5, Fish, Salmon, 15  
1, Fruit, Apple, 14  
2, Fruit, Orange, 27  

Table created from sorted DataView  
Bread, 23  
Fish, 12  
Fish, 15  
Fruit, 14  
Fruit, 27  

New table name: UniqueData  

注釈

使用可能な列のサブセットで個別の ToTable 値を取得し、返される の新しい名前を指定する必要がある場合は、このオーバーロードされた DataTableバージョンの メソッドを使用します。 個別の行や列のサブセットが必要ない場合は、「」を参照してください ToTable

こちらもご覧ください

適用対象

ToTable()

ソース:
DataView.cs
ソース:
DataView.cs
ソース:
DataView.cs

既存のDataView の行に基づく新しい DataTable を作成して返します。

public:
 System::Data::DataTable ^ ToTable();
public System.Data.DataTable ToTable ();
member this.ToTable : unit -> System.Data.DataTable
Public Function ToTable () As DataTable

戻り値

要求した行と列を格納する新しい DataTable インスタンス。

次のコンソール アプリケーションの例では、 を DataTable作成し、 を DataTable データで埋め、元のデータに基づいてフィルター処理されたビューを作成し、最後に、フィルター処理された行を含む を DataTable 作成します。

using System;
using System.Data;

class Program {
    static void Main() {
        DemonstrateDataView();
    }

    private static void DemonstrateDataView() {
        // Create a DataTable with three columns.
        DataTable table = new DataTable("NewTable");
        Console.WriteLine("Original table name: " + table.TableName);
        DataColumn column = new DataColumn("ID", typeof(System.Int32));
        table.Columns.Add(column);

        column = new DataColumn("Category", typeof(System.String));
        table.Columns.Add(column);

        column = new DataColumn("Product", typeof(System.String));
        table.Columns.Add(column);

        column = new DataColumn("QuantityInStock", typeof(System.Int32));
        table.Columns.Add(column);

        // Add some items.
        DataRow row = table.NewRow();
        row.ItemArray = new object[] { 1, "Fruit", "Apple", 14 };
        table.Rows.Add(row);

        row = table.NewRow();
        row.ItemArray = new object[] { 2, "Fruit", "Orange", 27 };
        table.Rows.Add(row);

        row = table.NewRow();
        row.ItemArray = new object[] { 3, "Bread", "Muffin", 23 };
        table.Rows.Add(row);

        row = table.NewRow();
        row.ItemArray = new object[] { 4, "Fish", "Salmon", 12 };
        table.Rows.Add(row);

        // Mark all rows as "accepted". Not really required
        // for this particular example.
        table.AcceptChanges();

        // Print current table values.
        PrintTableOrView(table, "Current Values in Table");

        DataView view = new DataView(table);
        view.RowFilter = "QuantityInStock > 15";
        PrintTableOrView(view, "Current Values in View");

        DataTable newTable = view.ToTable();
        PrintTableOrView(newTable, "Table created from filtered DataView");
        Console.WriteLine("New table name: " + newTable.TableName);

        Console.WriteLine("Press any key to continue.");
        Console.ReadKey();
    }

    private static void PrintTableOrView(DataView dv, string label) {
        System.IO.StringWriter sw;
        string output;
        DataTable table = dv.Table;

        Console.WriteLine(label);

        // Loop through each row in the view.
        foreach (DataRowView rowView in dv) {
            sw = new System.IO.StringWriter();

            // Loop through each column.
            foreach (DataColumn col in table.Columns) {
                // Output the value of each column's data.
                sw.Write(rowView[col.ColumnName].ToString() + ", ");
            }

            output = sw.ToString();

            // Trim off the trailing ", ", so the output looks correct.
            if (output.Length > 2)
                output = output.Substring(0, output.Length - 2);

            // Display the row in the console window.
            Console.WriteLine(output);
        }
        Console.WriteLine();
    }

    private static void PrintTableOrView(DataTable table, string label) {
        System.IO.StringWriter sw;
        string output;

        Console.WriteLine(label);

        // Loop through each row in the table.
        foreach (DataRow row in table.Rows) {
            sw = new System.IO.StringWriter();

            // Loop through each column.
            foreach (DataColumn col in table.Columns) {
                // Output the value of each column's data.
                sw.Write(row[col].ToString() + ", ");
            }

            output = sw.ToString();

            // Trim off the trailing ", ", so the output looks correct.
            if (output.Length > 2)
                output = output.Substring(0, output.Length - 2);

            // Display the row in the console window.
            Console.WriteLine(output);
        }
        Console.WriteLine();
    }
}
Private Sub DemonstrateDataView()
    ' Create a DataTable with three columns.
    Dim table As New DataTable("NewTable")
    Console.WriteLine("Original table name: " & table.TableName)
    Dim column As New DataColumn("ID", GetType(System.Int32))
    table.Columns.Add(column)

    column = New DataColumn("Category", GetType(System.String))
    table.Columns.Add(column)

    column = New DataColumn("Product", GetType(System.String))
    table.Columns.Add(column)

    column = New DataColumn("QuantityInStock", GetType(System.Int32))
    table.Columns.Add(column)

    ' Add some items.
    Dim row As DataRow = table.NewRow()
    row.ItemArray = New Object() {1, "Fruit", "Apple", 14}
    table.Rows.Add(row)

    row = table.NewRow()
    row.ItemArray = New Object() {2, "Fruit", "Orange", 27}
    table.Rows.Add(row)

    row = table.NewRow()
    row.ItemArray = New Object() {3, "Bread", "Muffin", 23}
    table.Rows.Add(row)

    row = table.NewRow()
    row.ItemArray = New Object() {4, "Fish", "Salmon", 12}
    table.Rows.Add(row)

    ' Mark all rows as "accepted". Not required
    ' for this particular example.
    table.AcceptChanges()

    ' Print current table values.
    PrintTableOrView(table, "Current Values in Table")

    Dim view As New DataView(table)
    view.RowFilter = "QuantityInStock > 15"
    PrintTableOrView(view, "Current Values in View")

    Dim newTable As DataTable = view.ToTable()
    PrintTableOrView(newTable, "Table created from filtered DataView")
    Console.WriteLine("New table name: " & newTable.TableName)

    Console.WriteLine("Press any key to continue.")
    Console.ReadKey()
End Sub

Private Sub PrintTableOrView(ByVal dv As DataView, ByVal label As String)
    Dim sw As System.IO.StringWriter
    Dim output As String
    Dim table As DataTable = dv.Table

    Console.WriteLine(label)

    ' Loop through each row in the view.
    For Each rowView As DataRowView In dv
        sw = New System.IO.StringWriter

        ' Loop through each column.
        For Each col As DataColumn In table.Columns
            ' Output the value of each column's data.
            sw.Write(rowView(col.ColumnName).ToString() & ", ")
        Next
        output = sw.ToString
        ' Trim off the trailing ", ", so the output looks correct.
        If output.Length > 2 Then
            output = output.Substring(0, output.Length - 2)
        End If
        ' Display the row in the console window.
        Console.WriteLine(output)
    Next
    Console.WriteLine()
End Sub

Private Sub PrintTableOrView(ByVal table As DataTable, ByVal label As String)
    Dim sw As System.IO.StringWriter
    Dim output As String

    Console.WriteLine(label)

    ' Loop through each row in the table.
    For Each row As DataRow In table.Rows
        sw = New System.IO.StringWriter
        ' Loop through each column.
        For Each col As DataColumn In table.Columns
            ' Output the value of each column's data.
            sw.Write(row(col).ToString() & ", ")
        Next
        output = sw.ToString
        ' Trim off the trailing ", ", so the output looks correct.
        If output.Length > 2 Then
            output = output.Substring(0, output.Length - 2)
        End If
        ' Display the row in the console window.
        Console.WriteLine(output)
    Next
    Console.WriteLine()
End Sub

この例では、次のテキストがコンソール ウィンドウに表示されます。

Original table name: NewTable  
Current Values in Table  
1, Fruit, Apple, 14  
2, Fruit, Orange, 27  
3, Bread, Muffin, 23  
4, Fish, Salmon, 12  

Current Values in View  
2, Fruit, Orange, 27  
3, Bread, Muffin, 23  

Table created from filtered DataView  
2, Fruit, Orange, 27  
3, Bread, Muffin, 23  

New table name: NewTable  

注釈

このメソッドでは出力 DataTableの名前を指定できないため、その名前はソース DataTableの名前と同じです。 このメソッドでは使用可能な列のサブセットを指定できないため、出力テーブルには入力テーブルと同じ列が含まれます。

こちらもご覧ください

適用対象