Sort Property

DataView.Sort Property


Gets or sets the sort column or columns, and sort order for the DataView.

Namespace:   System.Data
Assembly:  System.Data (in System.Data.dll)

public string Sort { get; set; }

Property Value

Type: System.String

A 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

      // Add data 
      locationTable.Rows.Add("Washington", "98052");
      locationTable.Rows.Add("California", "90001");
      locationTable.Rows.Add("Hawaii", "96807");
      locationTable.Rows.Add("Hawaii", "96801");

      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
Return to top
© 2015 Microsoft