26/03/2016 GridView.Sorting Event (System.Web.UI.WebControls)
https://msdn.microsoft.com/en­us/library/system.web.ui.webcontrols.gridview.sorting(v=vs.110).aspx 1/4
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﴿
Syntax
Remarks
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 NIB: Consuming Events.
Examples
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.
.NET Framework ﴾current version﴿
public event GridViewSortEventHandler Sorting 
<%@ Page Language="C#" %> 
<%@ Import Namespace="System.Data" %> 
<!DOCTYPE html PUBLIC "‐//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/
<script runat="server"> 
  protected void TaskGridView_Sorting(object sender, GridViewSortEventArgs e) 
  { 
    //Retrieve the table from the session object. 
    DataTable dt = Session["TaskTable"] as DataTable; 
    if (dt != null) 
    { 
      //Sort the data. 
      dt.DefaultView.Sort = e.SortExpression + " " + GetSortDirection(e.SortExpression); 
      TaskGridView.DataSource = Session["TaskTable"]; 
      TaskGridView.DataBind(); 
    } 
C#
C#
26/03/2016 GridView.Sorting Event (System.Web.UI.WebControls)
https://msdn.microsoft.com/en­us/library/system.web.ui.webcontrols.gridview.sorting(v=vs.110).aspx 2/4
  } 
  private string GetSortDirection(string column) 
  { 
    // By default, set the sort direction to ascending. 
    string sortDirection = "ASC"; 
    // Retrieve the last column that was sorted. 
    string sortExpression = ViewState["SortExpression"] as string; 
    if (sortExpression != null) 
    { 
      // Check if the same column is being sorted. 
      // Otherwise, the default value can be returned. 
      if (sortExpression == column) 
      { 
        string lastDirection = ViewState["SortDirection"] as string; 
        if ((lastDirection != null) && (lastDirection == "ASC")) 
        { 
          sortDirection = "DESC"; 
        } 
      } 
    } 
    // Save new values in ViewState. 
    ViewState["SortDirection"] = sortDirection; 
    ViewState["SortExpression"] = column; 
    return sortDirection; 
  } 
  protected void Page_Load(object sender, EventArgs e) 
  { 
    if (!Page.IsPostBack) 
    { 
      // Create a new table. 
      DataTable taskTable = new DataTable("TaskList"); 
      // Create the columns. 
      taskTable.Columns.Add("Id", typeof(int)); 
      taskTable.Columns.Add("Description", typeof(string)); 
      //Add data to the new table.
      for (int i = 0; i < 10; i++) 
      { 
        DataRow tableRow = taskTable.NewRow(); 
        tableRow["Id"] = i; 
        tableRow["Description"] = "Task " + (10 ‐ i).ToString(); 
        taskTable.Rows.Add(tableRow); 
      } 
      //Persist the table in the Session object. 
      Session["TaskTable"] = taskTable; 
      //Bind the GridView control to the data source. 
      TaskGridView.DataSource = Session["TaskTable"]; 
      TaskGridView.DataBind(); 
    } 
  } 
</script> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
26/03/2016 GridView.Sorting Event (System.Web.UI.WebControls)
https://msdn.microsoft.com/en­us/library/system.web.ui.webcontrols.gridview.sorting(v=vs.110).aspx 3/4
The following example demonstrates how to use the Sorting event to cancel a sorting operation.
    <title>Sorting example</title>
</head> 
<body> 
    <form id="form1" runat="server"> 
    <div> 
      <asp:GridView ID="TaskGridView" runat="server"  
        AllowSorting="true"  
        OnSorting="TaskGridView_Sorting" > 
      </asp:GridView> 
    </div> 
    </form> 
</body> 
</html> 
<%@ Page language="C#" %> 
<!DOCTYPE html PUBLIC "‐//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1‐transitional.dtd"> 
<script runat="server"> 
  void CustomersGridView_Sorting(Object sender, GridViewSortEventArgs e) 
  { 
    // Cancel the sorting operation if the user attempts 
    // to sort by address. 
    if (e.SortExpression == "Address") 
    { 
      e.Cancel = true; 
      Message.Text = "You cannot sort by address."; 
      SortInformationLabel.Text = ""; 
    } 
    else 
    { 
      Message.Text = ""; 
    } 
  } 
  void CustomersGridView_Sorted(Object sender, EventArgs e) 
  { 
    // Display the sort expression and sort direction. 
    SortInformationLabel.Text = "Sorting by " + 
      CustomersGridView.SortExpression.ToString() + 
      " in " + CustomersGridView.SortDirection.ToString() + 
      " order."; 
  } 
</script> 
<html xmlns="http://www.w3.org/1999/xhtml" > 
  <head runat="server"> 
    <title>GridView Sorting Example</title> 
</head> 
<body> 
    <form id="form1" runat="server"> 
      <h3>GridView Sorting Example</h3> 
      <asp:label id="Message" 
        forecolor="Red" 
        runat="server"/> 
C#
26/03/2016 GridView.Sorting Event (System.Web.UI.WebControls)
https://msdn.microsoft.com/en­us/library/system.web.ui.webcontrols.gridview.sorting(v=vs.110).aspx 4/4
Version Information
.NET Framework
Available since 2.0
See Also
Sort
GridViewSortEventArgs
AllowSorting
OnSorting
Sorted
OnSorted
GridView Class
System.Web.UI.WebControls Namespace
NIB: Consuming Events
Sorting Data in a GridView Web Server Control
Return to top
© 2016 Microsoft
      <br/> 
      <asp:label id="SortInformationLabel" 
        forecolor="Navy" 
        runat="server"/> 
      <br/>   
      <asp:gridview id="CustomersGridView"  
        datasourceid="CustomersSource"  
        autogeneratecolumns="true"
        allowpaging="true" 
        emptydatatext="No data available."  
        allowsorting="true" 
        onsorting="CustomersGridView_Sorting" 
        onsorted="CustomersGridView_Sorted"   
        runat="server"> 
      </asp:gridview> 
      <!‐‐ 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], [Cou
        connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>"  
        runat="server"/> 
    </form> 
  </body> 
</html> 

Grid view sorting-c-ode