DataView.ToTable Methode
Definitie
Belangrijk
Bepaalde informatie heeft betrekking op een voorlopige productversie die aanzienlijk kan worden gewijzigd voordat deze wordt uitgebracht. Microsoft biedt geen enkele expliciete of impliciete garanties met betrekking tot de informatie die hier wordt verstrekt.
Overloads
| Name | Description |
|---|---|
| ToTable(Boolean, String[]) |
Hiermee maakt en retourneert u een nieuwe DataTable op basis van rijen in een bestaande DataView. |
| ToTable(String) |
Hiermee maakt en retourneert u een nieuwe DataTable op basis van rijen in een bestaande DataView. |
| ToTable(String, Boolean, String[]) |
Hiermee maakt en retourneert u een nieuwe DataTable op basis van rijen in een bestaande DataView. |
| ToTable() |
Hiermee maakt en retourneert u een nieuwe DataTable op basis van rijen in een bestaande DataView. |
Opmerkingen
De geretourneerde DataTable behoudt de waarden van de volgende eigenschappen uit de bron DataTable: Namespace, Prefix, Locale, en CaseSensitive. Elke kolom in het resulterende DataTable bestand bevat een identieke kopie van alle bijbehorende eigenschappen in de onderliggende DataTablekolom.
De DataRow exemplaren in het geretourneerde DataTable bestand bevatten waarden die consistent zijn met CLR-semantiek. Voor alle gegevenstypen, naast door de gebruiker gedefinieerde typen, worden de bijbehorende kolomwaarden gekopieerd op waarde. Voor door de gebruiker gedefinieerde gegevenstypen worden de kolomgegevens gekopieerd per verwijzing.
ToTable(Boolean, String[])
- Bron:
- DataView.cs
- Bron:
- DataView.cs
- Bron:
- DataView.cs
- Bron:
- DataView.cs
- Bron:
- 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
Parameters
- distinct
- Boolean
Als true, de geretourneerde DataTable bevat rijen met afzonderlijke waarden voor alle kolommen. De standaardwaarde is false.
- columnNames
- String[]
Een tekenreeksmatrix die een lijst bevat met de kolomnamen die moeten worden opgenomen in de geretourneerde DataTablematrix. De DataTable kolom bevat de opgegeven kolommen in de volgorde waarin ze in deze matrix worden weergegeven.
Retouren
Een nieuw DataTable exemplaar met de aangevraagde rijen en kolommen.
Voorbeelden
In het volgende consoletoepassingsvoorbeeld wordt een DataTable, gevuld met gegevens DataTable , gesorteerd DataViewen ten slotte wordt een DataTable met slechts twee kolommen gemaakt, beperkt tot rijen waarin alle waarden uniek zijn.
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
In het voorbeeld ziet u de volgende uitvoer in het consolevenster:
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
Opmerkingen
Omdat u met deze methode niet de naam voor de uitvoer DataTablekunt opgeven, is de naam hetzelfde als de naam van de bron DataTable.
Van toepassing op
ToTable(String)
- Bron:
- DataView.cs
- Bron:
- DataView.cs
- Bron:
- DataView.cs
- Bron:
- DataView.cs
- Bron:
- 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
Parameters
Retouren
Een nieuw DataTable exemplaar met de aangevraagde rijen en kolommen.
Voorbeelden
In het volgende voorbeeld van de consoletoepassing maakt u een DataTable, vult u de DataTable met gegevens, maakt u een gefilterde weergave op basis van de oorspronkelijke gegevens en maakt u ten slotte een DataTable met een nieuwe naam die de gefilterde rijen bevat.
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
In het voorbeeld wordt de volgende tekst weergegeven in het consolevenster:
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
Opmerkingen
Omdat u met deze methode geen subset van beschikbare kolommen kunt opgeven, bevat de uitvoertabel dezelfde kolommen als de invoertabel.
Zie ook
Van toepassing op
ToTable(String, Boolean, String[])
- Bron:
- DataView.cs
- Bron:
- DataView.cs
- Bron:
- DataView.cs
- Bron:
- DataView.cs
- Bron:
- 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
Parameters
- distinct
- Boolean
Als true, de geretourneerde DataTable bevat rijen met afzonderlijke waarden voor alle kolommen. De standaardwaarde is false.
- columnNames
- String[]
Een tekenreeksmatrix die een lijst bevat met de kolomnamen die moeten worden opgenomen in de geretourneerde DataTablematrix. De DataTable kolom bevat de opgegeven kolommen in de volgorde waarin ze in deze matrix worden weergegeven.
Retouren
Een nieuw DataTable exemplaar met de aangevraagde rijen en kolommen.
Voorbeelden
In het volgende consoletoepassingsvoorbeeld wordt een DataTable, gevuld DataTable met gegevens, gesorteerd DataViewen ten slotte wordt een DataTable nieuwe naam gemaakt die slechts twee kolommen bevat, beperkt tot rijen waarin alle waarden uniek zijn.
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
In het voorbeeld ziet u de volgende uitvoer in het consolevenster:
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
Opmerkingen
Gebruik deze overbelaste versie van de ToTable methode als u afzonderlijke waarden moet ophalen in een subset van beschikbare kolommen, waarbij u een nieuwe naam opgeeft voor de geretourneerde DataTable. Zie als u geen afzonderlijke rijen of een subset kolommen ToTablenodig hebt.
Zie ook
Van toepassing op
ToTable()
- Bron:
- DataView.cs
- Bron:
- DataView.cs
- Bron:
- DataView.cs
- Bron:
- DataView.cs
- Bron:
- DataView.cs
public:
System::Data::DataTable ^ ToTable();
public System.Data.DataTable ToTable();
member this.ToTable : unit -> System.Data.DataTable
Public Function ToTable () As DataTable
Retouren
Een nieuw DataTable exemplaar met de aangevraagde rijen en kolommen.
Voorbeelden
Het volgende consoletoepassingsvoorbeeld maakt een DataTable, vult de DataTable met gegevens, maakt een gefilterde weergave op basis van de oorspronkelijke gegevens en maakt ten slotte een DataTable weergave die de gefilterde rijen bevat.
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
In het voorbeeld wordt de volgende tekst weergegeven in het consolevenster:
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
Opmerkingen
Omdat u met deze methode niet de naam voor de uitvoer DataTablekunt opgeven, is de naam hetzelfde als de naam van de bron DataTable. Omdat u met deze methode geen subset van beschikbare kolommen kunt opgeven, bevat de uitvoertabel dezelfde kolommen als de invoertabel.