DataView.ToTable Metod
Definition
Viktigt
En del information gäller för förhandsversionen av en produkt och kan komma att ändras avsevärt innan produkten blir allmänt tillgänglig. Microsoft lämnar inga garantier, uttryckliga eller underförstådda, avseende informationen som visas här.
Överlagringar
| Name | Description |
|---|---|
| ToTable(Boolean, String[]) |
Skapar och returnerar en ny DataTable baserat på rader i en befintlig DataView. |
| ToTable(String) |
Skapar och returnerar en ny DataTable baserat på rader i en befintlig DataView. |
| ToTable(String, Boolean, String[]) |
Skapar och returnerar en ny DataTable baserat på rader i en befintlig DataView. |
| ToTable() |
Skapar och returnerar en ny DataTable baserat på rader i en befintlig DataView. |
Kommentarer
Returnerade DataTable underhåller värdena för följande egenskaper från källan DataTable: Namespace, Prefix, Locale, och CaseSensitive. Varje kolumn i resultatet DataTable innehåller en identisk kopia av alla motsvarande egenskaper i den underliggande DataTable.
Instanserna DataRow i den returnerade DataTable innehåller värden som är konsekventa med CLR-semantik. För alla datatyper förutom användardefinierade typer kopieras alltså motsvarande kolumnvärden efter värde. För användardefinierade datatyper kopieras kolumndata med referens.
ToTable(Boolean, String[])
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
Parametrar
- distinct
- Boolean
Om trueinnehåller den returnerade DataTable rader som har distinkta värden för alla dess kolumner. Standardvärdet är false.
- columnNames
- String[]
En strängmatris som innehåller en lista över de kolumnnamn som ska ingå i den returnerade DataTable. DataTable Innehåller de angivna kolumnerna i den ordning de visas i den här matrisen.
Returer
En ny DataTable instans som innehåller de begärda raderna och kolumnerna.
Exempel
Följande konsolprogramexempel skapar en DataTable, fyller med data, sorterar DataTableDataView, och skapar slutligen en DataTable med bara två kolumner, begränsat till rader där alla värden är unika.
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
Exemplet visar följande utdata i konsolfönstret:
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
Kommentarer
Eftersom den här metoden inte låter dig ange namnet på utdata DataTableär dess namn samma som namnet på källan DataTable.
Gäller för
ToTable(String)
public:
System::Data::DataTable ^ ToTable(System::String ^ tableName);
public System.Data.DataTable ToTable(string tableName);
member this.ToTable : string -> System.Data.DataTable
Public Function ToTable (tableName As String) As DataTable
Parametrar
Returer
En ny DataTable instans som innehåller de begärda raderna och kolumnerna.
Exempel
I följande konsolprogramexempel skapas en DataTable, fyller DataTable med data, skapar en filtrerad vy baserat på ursprungliga data och skapar slutligen en DataTable med ett nytt namn som innehåller de filtrerade raderna.
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
Exemplet visar följande text i konsolfönstret:
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
Kommentarer
Eftersom den här metoden inte låter dig ange en delmängd av tillgängliga kolumner innehåller utdatatabellen samma kolumner som indatatabellen.
Se även
Gäller för
ToTable(String, Boolean, String[])
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);
member this.ToTable : string * bool * string[] -> System.Data.DataTable
Public Function ToTable (tableName As String, distinct As Boolean, ParamArray columnNames As String()) As DataTable
Parametrar
- distinct
- Boolean
Om trueinnehåller den returnerade DataTable rader som har distinkta värden för alla dess kolumner. Standardvärdet är false.
- columnNames
- String[]
En strängmatris som innehåller en lista över de kolumnnamn som ska ingå i den returnerade DataTable.
DataTable Innehåller de angivna kolumnerna i den ordning de visas i den här matrisen.
Returer
En ny DataTable instans som innehåller de begärda raderna och kolumnerna.
Exempel
I följande konsolprogramexempel skapas en DataTable, fyller med data, sorterar DataViewDataTable , och skapar slutligen ett DataTable med ett nytt namn som bara innehåller två kolumner, begränsat till rader där alla värden är unika.
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
Exemplet visar följande utdata i konsolfönstret:
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
Kommentarer
Använd den här överlagrade versionen av ToTable metoden om du måste hämta distinkta värden i en delmängd av tillgängliga kolumner och ange ett nytt namn för den returnerade DataTable. Om du inte behöver distinkta rader eller en delmängd av kolumner kan du läsa ToTable.
Se även
Gäller för
ToTable()
public:
System::Data::DataTable ^ ToTable();
public System.Data.DataTable ToTable();
member this.ToTable : unit -> System.Data.DataTable
Public Function ToTable () As DataTable
Returer
En ny DataTable instans som innehåller de begärda raderna och kolumnerna.
Exempel
I följande konsolprogramexempel skapas en DataTable, fyller DataTable med data, skapar en filtrerad vy baserat på ursprungliga data och skapar slutligen en DataTable som innehåller de filtrerade raderna.
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
Exemplet visar följande text i konsolfönstret:
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
Kommentarer
Eftersom den här metoden inte låter dig ange namnet på utdata DataTableär dess namn samma som namnet på källan DataTable. Eftersom den här metoden inte låter dig ange en delmängd av tillgängliga kolumner innehåller utdatatabellen samma kolumner som indatatabellen.