This documentation is archived and is not being maintained.

GridView.Sorting Event

Occurs when the hyperlink to sort a column is clicked, but before the GridView control handles the sort operation.

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

Public Event Sorting As GridViewSortEventHandler
<asp:GridView OnSorting="GridViewSortEventHandler" />

The Sorting event is raised when the hyperlink to sort a column is clicked, but before the GridView control handles the sort operation. This enables you to provide an event-handling method that performs a custom routine, such as canceling the sorting operation, whenever this event occurs.

A GridViewSortEventArgs object is passed to the event-handling method, which enables you to determine the sort expression for the column and to indicate that the selection operation should be canceled. To cancel the selection operation, set the Cancel property of the GridViewSortEventArgs object to true.

For information about how to programmatically initiate a sort operation, see the Sort method.

For more information about handling events, see Consuming Events.

The following example demonstrates how to use the Sorting event to perform the sorting functionality when the GridView control is bound to a DataTable object by setting the DataSource property programmatically.

<%@ Page Language="VB" %>
<%@ Import Namespace="System.Data" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

<script runat="server">

  Protected Sub TaskGridView_Sorting(ByVal sender As Object, ByVal e As GridViewSortEventArgs)

    'Retrieve the table from the session object.
    Dim dt = TryCast(Session("TaskTable"), DataTable)

    If dt IsNot Nothing Then

      'Sort the data.
      dt.DefaultView.Sort = e.SortExpression & " " & GetSortDirection(e.SortExpression)
      TaskGridView.DataSource = Session("TaskTable")

    End If

  End Sub

  Private Function GetSortDirection(ByVal column As String) As String

    ' By default, set the sort direction to ascending.
    Dim sortDirection = "ASC"

    ' Retrieve the last column that was sorted.
    Dim sortExpression = TryCast(ViewState("SortExpression"), String)

    If sortExpression IsNot Nothing Then
      ' Check if the same column is being sorted.
      ' Otherwise, the default value can be returned.
      If sortExpression = column Then
        Dim lastDirection = TryCast(ViewState("SortDirection"), String)
        If lastDirection IsNot Nothing _
          AndAlso lastDirection = "ASC" Then

          sortDirection = "DESC"

        End If
      End If
    End If

    ' Save new values in ViewState.
    ViewState("SortDirection") = sortDirection
    ViewState("SortExpression") = column

    Return sortDirection

  End Function

  Protected Sub Page_Load()

    If Not Page.IsPostBack Then

      ' Create a new table.
      Dim taskTable As New DataTable("TaskList")

      ' Create the columns.
      taskTable.Columns.Add("Id", GetType(Integer))
      taskTable.Columns.Add("Description", GetType(String))

      'Add data to the new table.
      For i = 0 To 9
        Dim tableRow As DataRow = taskTable.NewRow()
        tableRow("Id") = i
        tableRow("Description") = "Task " & (10 - i)
      Next i

      'Persist the table in the Session object.
      Session("TaskTable") = taskTable

      'Bind the GridView control to the data source.
      TaskGridView.DataSource = Session("TaskTable")
    End If

  End Sub


<html xmlns="">
<head runat="server">
    <title>Sorting example</title>
    <form id="form1" runat="server">

      <asp:GridView ID="TaskGridView" runat="server" 
        OnSorting="TaskGridView_Sorting" >


The following example demonstrates how to use the Sorting event to cancel a sorting operation.

<%@ Page language="VB" %>

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

  Sub CustomersGridView_Sorting(sender As Object, e As GridViewSortEventArgs)

    ' Cancel the sorting operation if the user attempts
    ' to sort by address.
    If e.SortExpression = "Address" Then

      e.Cancel = True
      Message.Text = "You cannot sort by address."
      SortInformationLabel.Text = ""


      Message.Text = ""

    End If

  End Sub

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

    ' Display the sort expression and sort direction.
    SortInformationLabel.Text = "Sorting by " & _
      CustomersGridView.SortExpression.ToString() & _
      " in " & CustomersGridView.SortDirection.ToString() & _
      " order."

  End Sub


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

      <h3>GridView Sorted and Sorting Example</h3>

      <asp:label id="Message"


      <asp:label id="SortInformationLabel"


      <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

Supported in: 4, 3.5, 3.0, 2.0

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.