asp.net.ph

DataGrid.SortCommand Event

System.Web.UI.WebControls Namespace   DataGrid Class


Occurs when a column is sorted.

[ VB ]
Public Event SortCommand As DataGridSortCommandEventHandler

[ C# ]
public event DataGridSortCommandEventHandler SortCommand;

[ C++ ]
public: __event DataGridSortCommandEventHandler* SortCommand;

In [ JScript ], you can handle the events defined by a class, but you cannot define your own.

Remarks

The SortCommand event is raised whenever any of the LinkButton controls used for sorting a DataGrid is clicked.

Event Data

The method assigned to handle the event is passed an argument of type DataGridSortCommandEventArgs object containing data related to this event. The following DataGridSortCommandEventArgs properties provide information specific to this event.

Property Description
CommandSource Gets the source of the command.
SortExpression Gets the expression used to sort the DataGrid control.

Example

The following code snippets demonstrate how to specify and code a handler for the SortCommand event to enable sorting in a DataGrid control.

The below code shows how to designate a handler for the SortCommand event at design time. Note that the AllowSorting property must be set for sorting to be enabled.

<asp:datagrid id = "myGrid" runat = "server"
   ...
   allowsorting
   onSortCommand = "sortGrid" />

The below shows the typical code for the SortCommand handler.

<script language = "C#" runat = "server">
   string query = "SELECT CompanyName, ContactName, ... FROM Suppliers";

   void Page_Load ( Object src, EventArgs e ) {
      if ( !IsPostBack ) {
         DataView myView = fetchData ( query, "northwind" ).Tables [ 0 ].DefaultView;
         myView.Sort = "companyname";
         myGrid.DataSource = myView;
         myGrid.DataBind ( );
      }
   }

   void sortGrid ( Object src, DataGridSortCommandEventArgs e ) {
      DataView myView = fetchData ( query, "northwind" ).Tables [ 0 ].DefaultView;
      myView.Sort = e.SortExpression;
      myGrid.DataSource = myView;
      myGrid.DataBind ( );
   }
</script>
  C# VB

 Show me 

See Also

DataGrid Members   DataGridSortCommandEventArgs   DataGridSortCommandEventHandler Skip Navigation Links




Home
Suggested Reading


Previous page Back to top Next page

© 2000-2010 Rey Nuñez All rights reserved.

If you have any question, comment or suggestion
about this site, please send us a note

You can help support asp.net.ph