Export (0) Print
Expand All
Expand Minimize
2 out of 5 rated this helpful - Rate this topic

GridView.Sort Method

Sorts the GridView control based on the specified sort expression and direction.

Namespace:  System.Web.UI.WebControls
Assembly:  System.Web (in System.Web.dll)
public virtual void Sort(
	string sortExpression,
	SortDirection sortDirection


Type: System.String

The sort expression with which to sort the GridView control.

Type: System.Web.UI.WebControls.SortDirection

One of the SortDirection values.


The GridView control is bound to a data source control, but the DataSourceView that is associated with the data source is null.

Use the Sort method to programmatically sort the GridView control using the specified sort expression and direction. The sort expression specifies the column or columns with which to sort. To sort multiple columns, create a sort expression that contains a comma-separated list of field names. The sort direction indicates whether sorting is performed in ascending or descending order. This method is commonly used when you need to sort the GridView control from outside of the control, such as from a different control on the page. This method is also commonly used to programmatically set a default sort order for the GridView control when it is first rendered. Calling this method also raises the Sorted and Sorting events.

The following example demonstrates how to use the Sort method to programmatically sort the GridView control by multiple columns.

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

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

  void SortButton_Click(Object sender, EventArgs e)

    String expression = "";
    SortDirection direction;

    // Create the sort expression from the values selected 
    // by the user from the DropDownList controls. Multiple
    // columns can be sorted by creating a sort expression
    // that contains a comma-separated list of field names.
    expression = SortList1.SelectedValue + "," + SortList2.SelectedValue;

    //  Determine the sort direction. The sort direction
    // applies only to the second column sorted.
    switch (DirectionList.SelectedValue)
      case "Ascending":
        direction = SortDirection.Ascending;
      case "Descending":
        direction = SortDirection.Descending;
        direction = SortDirection.Ascending;

    // Use the Sort method to programmatically sort the GridView
    // control using the sort expression and direction.
    CustomersGridView.Sort(expression, direction);



<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>GridView Sort Example</title>
    <form id="form1" runat="server">

      <h3>GridView Sort Example</h3>

             Sort by:
            <asp:dropdownlist ID="SortList1"
              <asp:listitem Selected="true">CustomerID</asp:listitem>
          <td colspan="2">
            Then by:
              <asp:dropdownlist ID="SortList2"
                <asp:listitem Selected="true">CustomerID</asp:listitem>
             Sort order:      
            <asp:radiobuttonlist id="DirectionList"
              <asp:listitem selected="true">Ascending</asp:listitem>

      <asp:button id="SortButton"


      <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.5.1, 4.5, 4, 3.5, 3.0, 2.0

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
© 2014 Microsoft. All rights reserved.