Export (0) Print
Expand All
0 out of 10 rated this helpful - Rate this topic

GridViewSortEventArgs Class

Note: This class is new in the .NET Framework version 2.0.

Provides data for the Sorting event.

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

public class GridViewSortEventArgs : CancelEventArgs
public class GridViewSortEventArgs extends CancelEventArgs
public class GridViewSortEventArgs extends CancelEventArgs

The GridView control raises the Sorting event when a Sort button (a button with its CommandName property set to "Sort") is clicked, but before the GridView control handles the sort operation. This allows you to provide an event-handling method that performs a custom routine, such as canceling the sorting operation, whenever this event occurs.

NoteNote

Although any button within a GridView control with its CommandName property set to "Sort" will raise the Sorting event, Sort buttons typically appear in the header row of a GridView control.

A GridViewSortEventArgs object is passed to the event-handling method, which allows you to specify or determine the sort expression and sort direction in which to sort the GridView control. To determine the sort expression, use the SortExpression property. To determine the sort direction, use the SortDirection property. You can also cancel a sorting operation by setting the Cancel property to true.

For more information about handling events, see Consuming Events.

For a list of initial property values for an instance of GridViewSortEventArgs, see the GridViewSelectEventArgs constructor.

The following example demonstrates how to use the GridViewSortEventArgs object passed to the event-handling method to cancel the sorting operation when the user attempts to sort the address column in a GridView control.


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

<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>
  <body>
    <form runat="server">
        
      <h3>GridView Sorting Example</h3>

      <asp:label id="Message"
        forecolor="Red"
        runat="server"/>
        
      <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], [Country] From [Customers]"
        connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>" 
        runat="server"/>
        
    </form>
  </body>
</html>


System.Object
   System.EventArgs
     System.ComponentModel.CancelEventArgs
      System.Web.UI.WebControls.GridViewSortEventArgs
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

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

.NET Framework

Supported in: 2.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.