C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
GetTable: This is a custom method that returns a new DataTable. Before we can use DataView, we must have a complete table.
Note: The GetTable method returns a DataTable with four columns, each specifying an attribute of a dog—Weight, Name, Breed and Date.
DefaultView: In the Main method, the DefaultView is accessed. DefaultView is a property of type DataView. This means it has a Sort property.
Sort: We assign the Sort property to a string that matches one of the column names ("Weight").
StringsC# program that uses DataView
using System;
using System.Data;
class Program
{
static void Main()
{
//
// Specify the column to sort on.
//
DataTable table = GetTable();
table.DefaultView.Sort = "Weight";
//
// Display all records in the view.
//
DataView view = table.DefaultView;
Console.WriteLine("=== Sorted by weight ===");
for (int i = 0; i < view.Count; i++)
{
Console.WriteLine("{0}, {1}, {2}, {3}",
view[i][0],
view[i][1],
view[i][2],
view[i][3]);
}
//
// Now sort on the Name.
//
view.Sort = "Name";
//
// Display all records in the view.
//
Console.WriteLine("=== Sorted by name ===");
for (int i = 0; i < view.Count; i++)
{
Console.WriteLine("{0}, {1}, {2}, {3}",
view[i][0],
view[i][1],
view[i][2],
view[i][3]);
}
}
/// <summary>
/// This example method generates a DataTable.
/// </summary>
static DataTable GetTable()
{
//
// Here we create a DataTable with four columns.
//
DataTable table = new DataTable();
table.Columns.Add("Weight", typeof(int));
table.Columns.Add("Name", typeof(string));
table.Columns.Add("Breed", typeof(string));
table.Columns.Add("Date", typeof(DateTime));
//
// Here we add unsorted data to the DataTable and return.
//
table.Rows.Add(57, "Koko", "Shar Pei", DateTime.Now);
table.Rows.Add(130, "Fido", "Bullmastiff", DateTime.Now);
table.Rows.Add(92, "Alex", "Anatolian Shepherd Dog", DateTime.Now);
table.Rows.Add(25, "Charles", "Cavalier King Charles Spaniel", DateTime.Now);
table.Rows.Add(7, "Candy", "Yorkshire Terrier", DateTime.Now);
return table;
}
}
Output
=== Sorted by weight ===
7, Candy, Yorkshire Terrier, 4/5/2009 3:38:22 PM
25, Charles, Cavalier King Charles Spaniel, 4/5/2009 3:38:22 PM
57, Koko, Shar Pei, 4/5/2009 3:38:22 PM
92, Alex, Anatolian Shepherd Dog, 4/5/2009 3:38:22 PM
130, Fido, Bullmastiff, 4/5/2009 3:38:22 PM
=== Sorted by name ===
92, Alex, Anatolian Shepherd Dog, 4/5/2009 3:38:22 PM
7, Candy, Yorkshire Terrier, 4/5/2009 3:38:22 PM
25, Charles, Cavalier King Charles Spaniel, 4/5/2009 3:38:22 PM
130, Fido, Bullmastiff, 4/5/2009 3:38:22 PM
57, Koko, Shar Pei, 4/5/2009 3:38:22 PM
Output: In the first section, the Weight integer is ordered in ascending (low to high) order.
And: In the second section, the Name string is ordered in ascending ASCII (a - z) sort order.
DataTable: Where you populate your data, from the user or database—an in-memory representation.
DataView: Accessed with the DefaultView property on DataTable. DataViews allow you to filter and sort data, not store it.
DefaultView: Access this property on your DataTable instance. This is an instance of DataView.
Count: This is an instance property on all DataView instances. You can use this in a for-loop on the DataView.
Sort: This is a string property on every DataView. Assign this to a string containing the name of a column.