DataView.ToTable Metodo

Definizione

Crea e restituisce un nuovo oggetto DataTable in base alle righe in un oggetto DataView esistente.

Overload

ToTable(Boolean, String[])

Crea e restituisce un nuovo oggetto DataTable in base alle righe in un oggetto DataView esistente.

ToTable(String)

Crea e restituisce un nuovo oggetto DataTable in base alle righe in un oggetto DataView esistente.

ToTable(String, Boolean, String[])

Crea e restituisce un nuovo oggetto DataTable in base alle righe in un oggetto DataView esistente.

ToTable()

Crea e restituisce un nuovo oggetto DataTable in base alle righe in un oggetto DataView esistente.

Commenti

Il restituito DataTable mantiene i valori delle proprietà seguenti dall'origine DataTable: Namespace, Prefix, Localee CaseSensitive. Ogni colonna nell'oggetto risultante DataTable contiene una copia identica di tutte le proprietà corrispondenti nell'oggetto sottostante DataTable.

Le DataRow istanze restituite DataTable conterranno valori coerenti con la semantica CLR. Vale a dire, per tutti i tipi di dati oltre ai tipi definiti dall'utente, i valori di colonna corrispondenti vengono copiati in base al valore. Per i tipi di dati definiti dall'utente, i dati di colonna sono copiati da riferimento.

ToTable(Boolean, String[])

Origine:
DataView.cs
Origine:
DataView.cs
Origine:
DataView.cs

Crea e restituisce un nuovo oggetto DataTable in base alle righe in un oggetto DataView esistente.

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

Parametri

distinct
Boolean

Se true, l'oggetto DataTable restituito contiene righe che hanno valori distinti per tutte le colonne. Il valore predefinito è false.

columnNames
String[]

Matrice di stringhe che contiene un elenco dei nomi di colonna da includere nell'oggetto DataTable restituito. DataTable contiene le colonne specificate nell'ordine in cui sono visualizzate all'interno della matrice.

Restituisce

Nuova istanza dell'oggetto DataTable che contiene le righe e le colonne richieste.

Esempio

L'esempio di applicazione console seguente crea un oggetto , riempie i DataTable dati, ordina e DataViewinfine crea un DataTableDataTable oggetto con solo due colonne, limitato alle righe in cui tutti i valori sono univoci.

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

Nell'esempio viene visualizzato il seguente output nella finestra della console:

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  

Commenti

Poiché questo metodo non consente di specificare il nome per l'output DataTable, il nome è lo stesso del nome dell'origine DataTable.

Si applica a

ToTable(String)

Origine:
DataView.cs
Origine:
DataView.cs
Origine:
DataView.cs

Crea e restituisce un nuovo oggetto DataTable in base alle righe in un oggetto DataView esistente.

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

Parametri

tableName
String

Nome dell'oggetto DataTable restituito.

Restituisce

Nuova istanza dell'oggetto DataTable che contiene le righe e le colonne richieste.

Esempio

L'esempio di applicazione console seguente crea un oggetto , riempie i DataTable dati, crea una visualizzazione filtrata in base ai dati originali e infine crea DataTable un DataTablecon un nuovo nome contenente le righe filtrate.

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

Nell'esempio viene visualizzato il seguente testo nella finestra della console:

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  

Commenti

Poiché questo metodo non consente di specificare un subset di colonne disponibili, la tabella di output contiene le stesse colonne della tabella di input.

Vedi anche

Si applica a

ToTable(String, Boolean, String[])

Origine:
DataView.cs
Origine:
DataView.cs
Origine:
DataView.cs

Crea e restituisce un nuovo oggetto DataTable in base alle righe in un oggetto DataView esistente.

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

Parametri

tableName
String

Nome dell'oggetto DataTable restituito.

distinct
Boolean

Se true, l'oggetto DataTable restituito contiene righe che hanno valori distinti per tutte le colonne. Il valore predefinito è false.

columnNames
String[]

Matrice di stringhe che contiene un elenco dei nomi di colonna da includere nell'oggetto DataTable restituito. DataTable contiene le colonne specificate nell'ordine in cui sono visualizzate all'interno della matrice.

Restituisce

Nuova istanza dell'oggetto DataTable che contiene le righe e le colonne richieste.

Esempio

L'esempio di applicazione console seguente crea un oggetto , riempie i DataTable dati, ordina e DataViewinfine crea un DataTableDataTable oggetto con un nuovo nome che contiene solo due colonne, limitate alle righe in cui tutti i valori sono univoci.

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

Nell'esempio viene visualizzato il seguente output nella finestra della console:

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  

Commenti

Usare questa versione di overload del ToTable metodo se è necessario recuperare valori distinti in un subset di colonne disponibili, specificando un nuovo nome per il restituito DataTable. Se non sono necessarie righe distinte o un subset di colonne, vedere ToTable.

Vedi anche

Si applica a

ToTable()

Origine:
DataView.cs
Origine:
DataView.cs
Origine:
DataView.cs

Crea e restituisce un nuovo oggetto DataTable in base alle righe in un oggetto DataView esistente.

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

Restituisce

Nuova istanza dell'oggetto DataTable che contiene le righe e le colonne richieste.

Esempio

L'esempio di applicazione console seguente crea un DataTableoggetto , riempie i DataTable dati, crea una visualizzazione filtrata in base ai dati originali e infine crea un DataTable oggetto contenente le righe filtrate.

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

Nell'esempio viene visualizzato il seguente testo nella finestra della console:

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  

Commenti

Poiché questo metodo non consente di specificare il nome per l'output DataTable, il nome è lo stesso del nome dell'origine DataTable. Poiché questo metodo non consente di specificare un subset di colonne disponibili, la tabella di output contiene le stesse colonne della tabella di input.

Vedi anche

Si applica a