GridViewSortEventArgs.SortDirection Property


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

Gets or sets the direction in which to sort the GridView control.

Namespace:   System.Web.UI.WebControls
Assembly:  System.Web (in System.Web.dll)

public SortDirection SortDirection { get; set; }

Property Value

Type: System.Web.UI.WebControls.SortDirection

One of the SortDirection values.

When the Sorting event is raised, you can use the SortDirection property to determine the order in which the GridView control will sort its items (based on the sort expression) when the sort operation is performed. Because the Sorting event occurs before the sort operation, you can also use the SortDirection property to programmatically set the sort direction.

The following example demonstrates how to use the SortDirection property to determine the direction in which to sort a GridView control. If the user attempts to sort in descending order, the sort operation is canceled.

<%@ Page language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<script runat="server">

  void CustomersGridView_Sorting(Object sender, GridViewSortEventArgs e)
    // By default, the sort order toggles when the user clicks 
    // the same sort button repeatedly. For this example, cancel
    // the sort operation if the user attempts to sort in descending
    // order.
    if (e.SortDirection == SortDirection.Descending)
      e.Cancel = true;
      Message.Text = "Sorting in descending order is not supported.";
      Message.Text = "";


<html xmlns="" >
  <head runat="server">
    <title>GridViewSortEventArgs SortDirection Example</title>
    <form id="form1" runat="server">

      <h3>GridViewSortEventArgs SortDirection Example</h3>

      <asp:label id="Message"


      <asp:gridview id="CustomersGridView" 
        emptydatatext="No data available." 


      <!-- This example uses Microsoft SQL Server and connects  -->
      <!-- to the Northwind sample database. Use an ASP.NET     -->
      <!-- expression to retrieve the connection string value   -->
      <!-- from the Web.config file.                            -->
      <asp:sqldatasource id="CustomersSource"
        selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]"
        connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>" 


.NET Framework
Available since 2.0
Return to top