DataView.Sort Property
.NET Framework (current version)
Gets or sets the sort column or columns, and sort order for the DataView.
Assembly: System.Data (in System.Data.dll)
Property Value
Type: System.StringA string that contains the column name followed by "ASC" (ascending) or "DESC" (descending). Columns are sorted ascending by default. Multiple columns can be separated by commas.
If you do not explicitly specify sort criteria for DataView, the DataRowView objects in DataView are sorted based on the index of its corresponding DataRow in the DataTable.RowsDataRowCollection.
For more information, see DataViews.
The following example instructs the DataView to sort the table by two columns.
using System.Data; using System; public class A { static void Main(string[] args) { DataTable locationTable = new DataTable("Location"); // Add two columns locationTable.Columns.Add("State"); locationTable.Columns.Add("ZipCode"); // Add data locationTable.Rows.Add("Washington", "98052"); locationTable.Rows.Add("California", "90001"); locationTable.Rows.Add("Hawaii", "96807"); locationTable.Rows.Add("Hawaii", "96801"); locationTable.AcceptChanges(); Console.WriteLine("Rows in original order\n State \t\t ZipCode"); foreach (DataRow row in locationTable.Rows) { Console.WriteLine(" {0} \t {1}", row["State"], row["ZipCode"]); } // Create DataView DataView view = new DataView(locationTable); // Sort by State and ZipCode column in descending order view.Sort = "State ASC, ZipCode ASC"; Console.WriteLine("\nRows in sorted order\n State \t\t ZipCode"); foreach (DataRowView row in view) { Console.WriteLine(" {0} \t {1}", row["State"], row["ZipCode"]); } } }
.NET Framework
Available since 1.1
Available since 1.1
Show: