DataView.ToTable 方法

定義

根據現有 DataView 中的資料列,建立並傳回新的 DataTable

多載

ToTable(Boolean, String[])

根據現有 DataView 中的資料列,建立並傳回新的 DataTable

ToTable(String)

根據現有 DataView 中的資料列,建立並傳回新的 DataTable

ToTable(String, Boolean, String[])

根據現有 DataView 中的資料列,建立並傳回新的 DataTable

ToTable()

根據現有 DataView 中的資料列,建立並傳回新的 DataTable

備註

DataTable 回的 會從來源 DataTable維護下列屬性值: NamespacePrefixLocaleCaseSensitive。 結果 DataTable 中的每個數據行都包含基礎 DataTable中所有對應屬性的相同複本。

DataRowDataTable 的 實例將包含與 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[]

包含資料行名稱清單的字串陣列,並且要包含在傳回的 DataTableDataTable 包含指定資料行,其順序為在此陣列中出現的順序。

傳回

新的 DataTable 執行個體,其中包含所要求的資料列和資料行。

範例

下列主控台應用程式範例會建立 、 DataTable填滿 DataTable 數據、排序 DataView,最後只建立兩個數據行的,限制為所有值都是唯一 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);

    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[]

包含資料行名稱清單的字串陣列,並且要包含在傳回的 DataTableDataTable 包含指定資料行,其順序為在此陣列中出現的順序。

傳回

新的 DataTable 執行個體,其中包含所要求的資料列和資料行。

範例

下列主控台應用程式範例會 DataTable建立 、填入 DataTable 數據、排序 DataView,最後以新的名稱建立 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);

    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  

備註

如果您需要擷取可用數據行子集中的不同值,請針對傳DataTable回 的 指定新名稱,請使用這個多載版本的 ToTable 方法。 如果您不需要相異的資料列或資料行子集,請參閱 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的名稱相同。 因為這個方法不會讓您指定可用數據行的子集,所以輸出數據表會包含與輸入數據表相同的數據行。

另請參閱

適用於