Sorting / Sorted events

jqGrid sort data automatically, however it also provides the Sorting and Sorted events, where you can hook custom code. The Sorting event is a "before" event and fires before jqGrid does its own search logic. This event can be cancelled and you can implemenmt your own sorting logic. The Sorted event is an "after" event and fires after the actual sorting had been done.

You can use the JQGridSortEventArgs passed to the Sorting event to detect the column being sorted and the sort direction. Here is an example of overridiing the default sorting logic:

<trirand:JQGrid runat="server" ID="JQGrid1" Width="600px" OnSorting="CustomSortingGrid_Sorting">

protected void CustomSortingGrid_Sorting(object sender, Trirand.Web.UI.WebControls.JQGridSortEventArgs e) { // Cancel the default sorting, which essentially sorts the clicked column e.Cancel = true; // Get data from the datasource DataTable dt = GetData(); // Set sorting to always sort using the ID column // You can change that using the event arguments e.SortExpression (column name) and // e.SortDirection (asc / desc) dt.DefaultView.Sort = "CustomerID " + e.SortDirection.ToString().ToUpper(); // Rebind the grid using the sorted DataTable JQGrid1.DataSource = dt; JQGrid1.DataBind(); }


  Last Updated: 11/2/2009 | © Trirand, 2009