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中所有相应属性的相同副本。

DataRow返回DataTable的 中的实例将包含与 CLR 语义一致的值。 也就是说,对于除用户定义类型以外的所有数据类型,相应的列值都按值复制。 对于用户定义的数据类型,按引用复制列数据。

ToTable(Boolean, String[])

Source:
DataView.cs
Source:
DataView.cs
Source:
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 只包含两列的 ,限制为所有值唯一的行。

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)

Source:
DataView.cs
Source:
DataView.cs
Source:
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[])

Source:
DataView.cs
Source:
DataView.cs
Source:
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 一个包含两列的新名称,该名称仅限于所有值都唯一的行。

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  

注解

如果必须检索可用列的子集中的非重复值,并为返回DataTableToTable 指定一个新名称,请使用 方法的此重载版本。 如果不需要非重复行或列子集,请参阅 ToTable

另请参阅

适用于

ToTable()

Source:
DataView.cs
Source:
DataView.cs
Source:
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的名称相同。 由于此方法不允许指定可用列的子集,因此输出表包含与输入表相同的列。

另请参阅

适用于