DataView.ToTable Metoda

Definicja

Tworzy i zwraca nowy DataTable na podstawie wierszy w istniejącym DataViewobiekcie .

Przeciążenia

ToTable(Boolean, String[])

Tworzy i zwraca nowy DataTable na podstawie wierszy w istniejącym DataViewobiekcie .

ToTable(String)

Tworzy i zwraca nowy DataTable na podstawie wierszy w istniejącym DataViewobiekcie .

ToTable(String, Boolean, String[])

Tworzy i zwraca nowy DataTable na podstawie wierszy w istniejącym DataViewobiekcie .

ToTable()

Tworzy i zwraca nowy DataTable na podstawie wierszy w istniejącym DataViewobiekcie .

Uwagi

Zwrócony DataTable obiekt zachowuje wartości następujących właściwości ze źródła DataTable: Namespace, Prefix, Localei CaseSensitive. Każda kolumna w wyniku DataTable zawiera identyczną kopię wszystkich odpowiednich właściwości w bazowym DataTableobiekcie .

Wystąpienia DataRow zwrócone DataTable będą zawierać wartości zgodne z semantykami środowiska CLR. Oznacza to, że dla wszystkich typów danych oprócz typów zdefiniowanych przez użytkownika odpowiednie wartości kolumn są kopiowane według wartości. W przypadku typów danych zdefiniowanych przez użytkownika dane kolumny są kopiowane przez odwołanie.

ToTable(Boolean, String[])

Źródło:
DataView.cs
Źródło:
DataView.cs
Źródło:
DataView.cs

Tworzy i zwraca nowy DataTable na podstawie wierszy w istniejącym DataViewobiekcie .

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

Parametry

distinct
Boolean

Jeśli truezwracany DataTable element zawiera wiersze z unikatowymi wartościami dla wszystkich kolumn. Wartość domyślna to false.

columnNames
String[]

Tablica ciągów zawierająca listę nazw kolumn, które mają zostać uwzględnione w zwracanym DataTableobiekcie . Zawiera DataTable określone kolumny w kolejności, w której są wyświetlane w tej tablicy.

Zwraca

Nowe DataTable wystąpienie zawierające żądane wiersze i kolumny.

Przykłady

Poniższy przykład aplikacji konsolowej tworzy obiekt DataTable, wypełnia DataTable element danymi, sortuje DataViewelement , a na koniec tworzy DataTable obiekt z zaledwie dwiema kolumnami, z ograniczeniami do wierszy, w których wszystkie wartości są unikatowe.

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

W przykładzie zostaną wyświetlone następujące dane wyjściowe w oknie konsoli:

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  

Uwagi

Ponieważ ta metoda nie pozwala określić nazwy danych wyjściowych DataTable, jej nazwa jest taka sama jak nazwa źródła DataTable.

Dotyczy

ToTable(String)

Źródło:
DataView.cs
Źródło:
DataView.cs
Źródło:
DataView.cs

Tworzy i zwraca nowy DataTable na podstawie wierszy w istniejącym DataViewobiekcie .

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

Parametry

tableName
String

Nazwa zwróconego elementu DataTable.

Zwraca

Nowe DataTable wystąpienie zawierające żądane wiersze i kolumny.

Przykłady

Poniższy przykład aplikacji konsolowej DataTabletworzy obiekt , wypełnia dane DataTable danymi, tworzy filtrowany widok na podstawie oryginalnych danych, a na koniec tworzy obiekt DataTable o nowej nazwie zawierającej przefiltrowane wiersze.

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

W przykładzie zostanie wyświetlony następujący tekst w oknie konsoli:

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  

Uwagi

Ponieważ ta metoda nie umożliwia określenia podzestawu dostępnych kolumn, tabela wyjściowa zawiera te same kolumny co tabela wejściowa.

Zobacz też

Dotyczy

ToTable(String, Boolean, String[])

Źródło:
DataView.cs
Źródło:
DataView.cs
Źródło:
DataView.cs

Tworzy i zwraca nowy DataTable na podstawie wierszy w istniejącym DataViewobiekcie .

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

Parametry

tableName
String

Nazwa zwróconego elementu DataTable.

distinct
Boolean

Jeśli truezwracany DataTable element zawiera wiersze z unikatowymi wartościami dla wszystkich kolumn. Wartość domyślna to false.

columnNames
String[]

Tablica ciągów zawierająca listę nazw kolumn, które mają zostać uwzględnione w zwracanym DataTableobiekcie . Zawiera DataTable określone kolumny w kolejności, w której są wyświetlane w tej tablicy.

Zwraca

Nowe DataTable wystąpienie zawierające żądane wiersze i kolumny.

Przykłady

Poniższy przykład aplikacji konsolowej tworzy element DataTable, wypełnia DataTable element danymi, sortuje DataViewelement , a na koniec tworzy DataTable element z nową nazwą zawierającą tylko dwie kolumny, ograniczone do wierszy, w których wszystkie wartości są unikatowe.

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

W przykładzie zostaną wyświetlone następujące dane wyjściowe w oknie konsoli:

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  

Uwagi

Użyj tej przeciążonej ToTable wersji metody , jeśli musisz pobrać unikatowe wartości w podzestawie dostępnych kolumn, określając nową nazwę zwracanego DataTableelementu . Jeśli nie potrzebujesz odrębnych wierszy ani podzestawu kolumn, zobacz ToTable.

Zobacz też

Dotyczy

ToTable()

Źródło:
DataView.cs
Źródło:
DataView.cs
Źródło:
DataView.cs

Tworzy i zwraca nowy DataTable na podstawie wierszy w istniejącym DataViewobiekcie .

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

Zwraca

Nowe DataTable wystąpienie zawierające żądane wiersze i kolumny.

Przykłady

Poniższy przykład aplikacji konsolowej DataTabletworzy obiekt , wypełnia dane DataTable danymi, tworzy filtrowany widok na podstawie oryginalnych danych, a na koniec tworzy obiekt DataTable zawierający przefiltrowane wiersze.

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

W przykładzie zostanie wyświetlony następujący tekst w oknie konsoli:

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  

Uwagi

Ponieważ ta metoda nie pozwala określić nazwy danych wyjściowych DataTable, jej nazwa jest taka sama jak nazwa źródła DataTable. Ponieważ ta metoda nie umożliwia określenia podzestawu dostępnych kolumn, tabela wyjściowa zawiera te same kolumny co tabela wejściowa.

Zobacz też

Dotyczy