There are three sorting modes in Telerik RadGrid:
|
Ascending |
Descending |
No Sort |
|

|

|

|
|
When you use the buttons that appear on the grid header item, clicking once will cause items to be sorted by that column ascending. Then an image will appear to indicate that the column is sorted ascending. |
Click on the header again to sort by that column descending. The sorting indicator will change to to indicate that column is sorted descending. |
The third click will remove the sorting by that column. |
Furthermore, if you would like to have only two-way sorting (ascending and descending), you can set the MasterTableView.SortExpressions.AllowNaturalSort or GridTableView.SortExpressions.AllowNaturalSort to false.
Additionally, to determine the new/previous sort order on sort command, you can wire the SortCommand handler of the grid and check the values for the e.NewSortOrder and e.OldSortOrder arguments.
Handling the sorting manually when clicking the column header text
In some occasions you may wish to control the sorting manually rather than using the automatic sequences (ASC/DESC/NONE or ASC/DESC) provided by Telerik RadGrid. This is easily achievable hooking the SortCommand event of the grid, cancelling the default action (e.Canceled = true) and changing the SortOrder attribute of the corresponding GridSortExpression. Then you merely need to inject once again the sort expression in the grid SortExpressions collection and rebind the GridTableView.
The forthcoming sample code demonstrates how to perform DESC/ASC/NONE sort for the EmployeeID column in the detail table of two-level hierarchy grid. You can determine the detail table for which the sorting command has been activated through the e.Item.OwnerTableView.DataMember (NET 1.x) or e.Item.OwnerTableView.DataSourceID (NET 2.x) attribute in the SortCommand handler of the grid:
| ASPX/ASCX |
Copy Code |
|
<rad:radgrid id="RadGrid1" runat="server" Width="95%" AutoGenerateColumns="False" PageSize="3" AllowSorting="True" AllowPaging="True"> <PagerStyle Mode="NumericPages" CssClass="Pager"></PagerStyle> <MasterTableView DataKeyNames="CustomerID" Width="100%"> <DetailTables> <rad:GridTableView DataKeyNames="OrderID" DataMember="Orders" Width="100%"> <ParentTableRelation> <rad:GridRelationFields DetailKeyField="CustomerID" MasterKeyField="CustomerID" /> </ParentTableRelation> <Columns> <rad:GridBoundColumn SortExpression="OrderID" HeaderText="OrderID" HeaderButtonType="TextButton" DataField= "OrderID"></rad:GridBoundColumn> <rad:GridBoundColumn SortExpression="OrderDate" HeaderText="Date Ordered" HeaderButtonType="TextButton" DataField= "OrderDate"></rad:GridBoundColumn> <rad:GridBoundColumn SortExpression="EmployeeID" HeaderText="EmployeeID" HeaderButtonType="TextButton" DataField= "EmployeeID"></rad:GridBoundColumn> </Columns> </rad:GridTableView> </DetailTables> <Columns> <rad:GridBoundColumn SortExpression="CustomerID" HeaderText="CustomerID" HeaderButtonType="TextButton" DataField= "CustomerID"></rad:GridBoundColumn> <rad:GridBoundColumn SortExpression="ContactName" HeaderText="Contact Name" HeaderButtonType="TextButton" DataField= "ContactName"></rad:GridBoundColumn> <rad:GridBoundColumn SortExpression="CompanyName" HeaderText="Company" HeaderButtonType="TextButton" DataField= "CompanyName"></rad:GridBoundColumn> </Columns> </MasterTableView> </rad:radgrid> |
And in the code-behind:
| C# |
Copy Code |
|
private void RadGrid1_NeedDataSource(object source, Telerik.WebControls.GridNeedDataSourceEventArgs e) { if (!e.IsFromDetailTable) { RadGrid1.DataSource = DataSourceHelperCS.GetDataTable("SELECT * FROM Customers"); }
RadGrid1.MasterTableView.DetailTables[0].DataSource = DataSourceHelperCS.GetDataTable("SELECT * FROM Orders");
}
private void RadGrid1_SortCommand(object source, Telerik.WebControls.GridSortCommandEventArgs e) { if(e.Item.OwnerTableView.DataMember == "Orders" && e.SortExpression == "EmployeeID") { e.Canceled = true; GridSortExpression expression = new GridSortExpression(); expression.FieldName = "EmployeeID";
if(e.Item.OwnerTableView.SortExpressions.Count == 0 || e.Item.OwnerTableView.SortExpressions[0].FieldName != e.SortExpression) { expression.SortOrder = GridSortOrder.Descending; } else if(e.Item.OwnerTableView.SortExpressions[0].SortOrder == GridSortOrder.Descending) { expression.SortOrder = GridSortOrder.Ascending; } else if(e.Item.OwnerTableView.SortExpressions[0].SortOrder == GridSortOrder.Ascending) { expression.SortOrder = GridSortOrder.None; }
e.Item.OwnerTableView.SortExpressions.AddSortExpression( expression ); e.Item.OwnerTableView.Rebind(); }
} |
| VB.NET |
Copy Code |
|
Private Sub RadGrid1_NeedDataSource([source] As Object, e As Telerik.WebControls.GridNeedDataSourceEventArgs) Handles RadGrid1.NeedDataSource If Not e.IsFromDetailTable Then RadGrid1.DataSource = DataSourceHelperCS.GetDataTable("SELECT * FROM Customers") End If
RadGrid1.MasterTableView.DetailTables(0).DataSource = DataSourceHelperCS.GetDataTable("SELECT * FROM Orders") End Sub
Private Sub RadGrid1_SortCommand([source] As Object, e As Telerik.WebControls.GridSortCommandEventArgs) Handles RadGrid1.SortCommand If e.Item.OwnerTableView.DataMember = "Orders" And e.SortExpression = "EmployeeID" Then e.Canceled = True Dim expression As New GridSortExpression() expression.FieldName = "EmployeeID"
If e.Item.OwnerTableView.SortExpressions.Count = 0 OrElse e.Item.OwnerTableView.SortExpressions(0).FieldName <> e.SortExpression Then
expression.SortOrder = GridSortOrder.Descending
ElseIf e.Item.OwnerTableView.SortExpressions(0).SortOrder = GridSortOrder.Descending Then
expression.SortOrder = GridSortOrder.Ascending
ElseIf e.Item.OwnerTableView.SortExpressions(0).SortOrder = GridSortOrder.Ascending Then
expression.SortOrder = GridSortOrder.None
End If
e.Item.OwnerTableView.SortExpressions.AddSortExpression(expression) e.Item.OwnerTableView.Rebind() End If End Sub |