Created
January 14, 2015 21:38
-
-
Save hoganlong/89be893055fa72c47fe5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
row = table.NewRow(); | |
row.ItemArray = new object[] { 5, "Fish", "Salmon", 15 }; | |
table.Rows.Add(row); | |
row = table.NewRow(); | |
row.ItemArray = new object[] { 7, "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(); | |
} | |
// Define other methods and classes here |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment