DataView.ToTable Méthode
Définition
Important
Certaines informations portent sur la préversion du produit qui est susceptible d’être en grande partie modifiée avant sa publication. Microsoft exclut toute garantie, expresse ou implicite, concernant les informations fournies ici.
Surcharges
ToTable(Boolean, String[]) |
Crée et retourne un nouveau DataTable basé sur les lignes d’un DataView existant. |
ToTable(String) |
Crée et retourne un nouveau DataTable basé sur les lignes d’un DataView existant. |
ToTable(String, Boolean, String[]) |
Crée et retourne un nouveau DataTable basé sur les lignes d’un DataView existant. |
ToTable() |
Crée et retourne un nouveau DataTable basé sur les lignes d’un DataView existant. |
Remarques
Le retourné DataTable conserve les valeurs des propriétés suivantes à partir de la source DataTable: Namespace, Prefix, Localeet CaseSensitive. Chaque colonne du résultat DataTable contient une copie identique de toutes les propriétés correspondantes dans le sous-jacent DataTable.
Les DataRow instances dans le retourné DataTable
contiennent des valeurs qui sont cohérentes avec la sémantique CLR. Autrement dit, pour tous les types de données en dehors des types définis par l’utilisateur, les valeurs de colonne correspondantes sont copiées par valeur. Pour les types de données définis par l’utilisateur, les données de colonne sont copiées par référence.
ToTable(Boolean, String[])
- Source:
- DataView.cs
- Source:
- DataView.cs
- Source:
- DataView.cs
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
Paramètres
- distinct
- Boolean
Si true
, le DataTable retourné contient des lignes dont les valeurs sont distinctes pour toutes ses colonnes. La valeur par défaut est false
.
- columnNames
- String[]
Tableau de chaînes qui contient une liste de noms de colonne à inclure dans le DataTable renvoyé. Le DataTable contient les colonnes spécifiées, dans l’ordre de leur apparition dans le tableau.
Retours
Nouvelle instance de DataTable qui contient les lignes et les colonnes demandées.
Exemples
L’exemple d’application console suivant crée un DataTable, remplit le DataTable avec des données, trie le DataView, puis crée un DataTable avec seulement deux colonnes, limité aux lignes dans lesquelles toutes les valeurs sont uniques.
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
L'exemple affiche la sortie suivante dans la fenêtre de 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
Remarques
Étant donné que cette méthode ne vous permet pas de spécifier le nom de la sortie DataTable, son nom est identique au nom de la source DataTable.
S’applique à
ToTable(String)
- Source:
- DataView.cs
- Source:
- DataView.cs
- Source:
- DataView.cs
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
Paramètres
Retours
Nouvelle instance de DataTable qui contient les lignes et les colonnes demandées.
Exemples
L’exemple d’application console suivant crée un DataTable, remplit le DataTable
avec des données, crée une vue filtrée basée sur les données d’origine, puis crée un DataTable
avec un nouveau nom qui contient les lignes filtrées.
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
L'exemple affiche le texte suivant dans la fenêtre de 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
Remarques
Étant donné que cette méthode ne vous permet pas de spécifier un sous-ensemble de colonnes disponibles, la table de sortie contient les mêmes colonnes que la table d’entrée.
Voir aussi
S’applique à
ToTable(String, Boolean, String[])
- Source:
- DataView.cs
- Source:
- DataView.cs
- Source:
- DataView.cs
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
Paramètres
- distinct
- Boolean
Si true
, le DataTable retourné contient des lignes dont les valeurs sont distinctes pour toutes ses colonnes. La valeur par défaut est false
.
- columnNames
- String[]
Tableau de chaînes qui contient une liste de noms de colonne à inclure dans le DataTable renvoyé. Le DataTable
contient les colonnes spécifiées, dans l’ordre de leur apparition dans le tableau.
Retours
Nouvelle instance de DataTable qui contient les lignes et les colonnes demandées.
Exemples
L’exemple d’application console suivant crée un DataTable, remplit le DataTable avec des données, trie le DataView, puis crée un DataTable avec un nouveau nom qui contient seulement deux colonnes, limité aux lignes dans lesquelles toutes les valeurs sont uniques.
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
L'exemple affiche la sortie suivante dans la fenêtre de 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
Remarques
Utilisez cette version surchargée de la ToTable méthode si vous devez récupérer des valeurs distinctes dans un sous-ensemble de colonnes disponibles, en spécifiant un nouveau nom pour le retourné DataTable. Si vous n’avez pas besoin de lignes distinctes ou d’un sous-ensemble de colonnes, consultez ToTable.
Voir aussi
S’applique à
ToTable()
- Source:
- DataView.cs
- Source:
- DataView.cs
- Source:
- DataView.cs
public:
System::Data::DataTable ^ ToTable();
public System.Data.DataTable ToTable ();
member this.ToTable : unit -> System.Data.DataTable
Public Function ToTable () As DataTable
Retours
Nouvelle instance de DataTable qui contient les lignes et les colonnes demandées.
Exemples
L’exemple d’application console suivant crée un DataTable, remplit le DataTable avec des données, crée une vue filtrée basée sur les données d’origine, puis crée un DataTable qui contient les lignes filtrées.
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
L'exemple affiche le texte suivant dans la fenêtre de 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
Remarques
Étant donné que cette méthode ne vous permet pas de spécifier le nom de la sortie DataTable, son nom est identique au nom de la source DataTable
. Étant donné que cette méthode ne vous permet pas de spécifier un sous-ensemble de colonnes disponibles, la table de sortie contient les mêmes colonnes que la table d’entrée.