GridView.SortDirection Property


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

Gets the sort direction of the column being sorted.

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

Public Overridable ReadOnly Property SortDirection As SortDirection

Property Value

Type: System.Web.UI.WebControls.SortDirection

One of the SortDirection values. The default is SortDirection.Ascending.

Use the SortDirection property to determine whether the column being sorted is sorted in ascending or descending order.


The GridView control has a built-in sorting feature that automatically sets this property. This property is typically used only when you need to programmatically determine the sort direction or when you are adding your own custom sorting functionality to a GridView control.

The following example demonstrates how to use the SortDirection property to programmatically determine the sort direction of a GridView control.

<%@ Page language="VB" %>

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

  Sub CustomersGridView_DataBound(ByVal sender As Object, ByVal e As EventArgs)

    ' Get the header row.
    Dim headerRow As GridViewRow = CustomersGridView.HeaderRow

    ' Get the footer row.
    Dim footerRow As GridViewRow = CustomersGridView.FooterRow

    ' Set the font color of the header and footer rows
    ' based on the sort direction. 
    Select Case CustomersGridView.SortDirection

      Case SortDirection.Ascending
        headerRow.ForeColor = System.Drawing.Color.Green
        footerRow.ForeColor = System.Drawing.Color.Green
      Case SortDirection.Descending
        headerRow.ForeColor = System.Drawing.Color.Red
        footerRow.ForeColor = System.Drawing.Color.Red
      Case Else
        headerRow.ForeColor = System.Drawing.Color.Black
        footerRow.ForeColor = System.Drawing.Color.Black

    End Select

    ' Display the sort order in the footer row.
    footerRow.Cells(0).Text = "Sort Order = " & CustomersGridView.SortDirection.ToString()

  End Sub


<html xmlns="" >
  <head runat="server">
    <title>GridView HeaderRow and FooterRow Example</title>
    <form id="form1" runat="server">

      <h3>GridView HeaderRow and FooterRow Example</h3>

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

        <headerstyle backcolor="LightCyan"

        <footerstyle backcolor="LightCyan"


      <!-- 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