We use DataView to sort a column on a DataTable. The GetTable method returns a table with 2 columns. Before we can use DataView, we must have a complete table.
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}", view[i][0], view[i][1]);
}
//
// 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}", view[i][0], view[i][1]);
}
}
static DataTable GetTable()
{
//
// Here we create a DataTable with 2 columns.
//
DataTable table = new DataTable();
table.Columns.Add(
"Weight", typeof(int));
table.Columns.Add(
"Name", typeof(string));
//
// Here we add unsorted data to the DataTable and return.
//
table.Rows.Add(57,
"Spot");
table.Rows.Add(130,
"Rover");
table.Rows.Add(92,
"Fred");
table.Rows.Add(25,
"Alice");
table.Rows.Add(7,
"Hunter");
return table;
}
}
=== Sorted by weight ===
7, Hunter
25, Alice
57, Spot
92, Fred
130, Rover
=== Sorted by name ===
25, Alice
92, Fred
7, Hunter
130, Rover
57, Spot