RadGrid for ASP.NET

Applying custom sort criteria Send comments on this topic.
Sorting > Applying custom sort criteria

Glossary Item Box

Sometimes you may need to skip the default sorting behavior (alphabetical ascending/descending/none for string columns, numeral ascending/descending/none for columns of type integer/double, etc.). RadGrid provides this option as well by setting the AllowCustomSorting property for the corresponding table. With custom sorting turned on, RadGrid will display the sorting icons but it will not actually sort the data. You will need to perform the custom sorting manually inside the SortCommand event handler.

The forthcoming code sample shows how to apply custom sort pattern for two columns - FirstName and LastName. The first column will be sorted in par with the length of the strings in its cells while the second column data will be sorted in reverse order (descending/ascending/none):

ASPX/ASCX Copy Code
<asp:Label ID="lblMessage" runat="server" BackColor="Beige" Text="Custom sort criteria"
 
EnableViewState="false" />
       
<br />
       
<br />
<
asp:Label ID="lblClmn1" runat="server" BackColor="Beige" Text="Sort by string length" EnableViewState="false"
 
Width="47%" />
<
asp:Label ID="lblClmn2" runat="server" BackColor="Beige" Text="Reverse sort" EnableViewState="false"
 
Width="48%"/>
<
br />
<
rad:RadGrid ID="RadGrid2" runat="server" Width="95%" AllowSorting="True" Skin="Desert"
 
AutoGenerateColumns="False" GridLines="None">
           
<MasterTableView Width="100%" AllowCustomSorting="true">
               
<Columns>
                   
<rad:GridBoundColumn HeaderText="First name" HeaderButtonType="TextButton" DataField="FirstName" />
                   
<rad:GridBoundColumn HeaderText="Last name" HeaderButtonType="TextButton" DataField="LastName" />
               
</Columns>
           
</MasterTableView>
</
rad:RadGrid>
C# Copy Code
private static DataTable GetDataTable(string query)
{
           OleDbConnection connection1 =
new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + System.Web.HttpContext.Current.Server.MapPath( "~/Grid/Data/Access/NWind.mdb"));
           OleDbDataAdapter adapter1 =
new OleDbDataAdapter();
           adapter1.SelectCommand =
new OleDbCommand(query, connection1);
           DataTable table1 =
new DataTable();
           connection1. Open();
           
try
           {
               
adapter1.Fill(table1);
           }
           
finally
           {
               
connection1. Close();
           }
            
return table1;
}
protected void RadGrid2_SortCommand(object source, GridSortCommandEventArgs e)
{
            
if (e.CommandArgument == "FirstName")
           {
               
switch (e.OldSortOrder)
               {
                    
case GridSortOrder.None:
                       e.Item.OwnerTableView.DataSource = GetDataTable(
"SELECT FirstName, LastName FROM Employees ORDER BY LEN(FirstName) ASC");
                       e.Item.OwnerTableView.Rebind();
                       
break;
                    
case GridSortOrder.Ascending:
                       e.Item.OwnerTableView.DataSource = GetDataTable(
"SELECT FirstName, LastName FROM Employees ORDER BY LEN(FirstName) DESC");
                       e.Item.OwnerTableView.Rebind();
                       
break;
                    
case GridSortOrder.Descending:
                       e.Item.OwnerTableView.DataSource = GetDataTable(
"SELECT FirstName, LastName FROM Employees");
                       e.Item.OwnerTableView.Rebind();
                       
break;
               }
          }
            
else if (e.CommandArgument == "LastName")
           {
               
switch (e.OldSortOrder)
               {
                    
case GridSortOrder.None:
                       e.Item.OwnerTableView.DataSource = GetDataTable(
"SELECT FirstName, LastName FROM Employees ORDER BY LastName DESC");
                       e.Item.OwnerTableView.Rebind();
                       
break;
                    
case GridSortOrder.Ascending:
                       e.Item.OwnerTableView.DataSource = GetDataTable(
"SELECT FirstName, LastName FROM Employees ORDER BY LastName ASC");
                       e.Item.OwnerTableView.Rebind();
                       
break;
                    
case GridSortOrder.Descending:
                       e.Item.OwnerTableView.DataSource = GetDataTable(
"SELECT FirstName, LastName FROM Employees");
                       e.Item.OwnerTableView.Rebind();
                       
break;
               }
           }
}
protected void RadGrid2_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
{
           RadGrid2.DataSource = GetDataTable(
"SELECT FirstName, LastName FROM Employees");
}

 

VB.NET Copy Code
Private Shared Function GetDataTable(ByVal query As String) As DataTable
             Dim connection1 As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + System.Web.HttpContext.Current.Server.MapPath( "~/Grid/Data/Access/NWind.mdb"))
             Dim adapter1 As New OleDbDataAdapter()
            adapter1.SelectCommand = New OleDbCommand(query, connection1)
             Dim table1 As New DataTable()
            connection1. Open()
            Try
                adapter1.Fill(table1)
            Finally
                connection1. Close()
             End Try
             Return table1
End Function
Protected Sub RadGrid2_SortCommand(ByVal source As Object, ByVal e As GridSortCommandEventArgs) Handles RadGrid2.SortCommand
             If e.CommandArgument = "FirstName" Then
                 Select Case e.OldSortOrder
                     Case GridSortOrder.None
                        e.Item.OwnerTableView.DataSource = GetDataTable("SELECT FirstName, LastName FROM Employees ORDER BY LEN(FirstName) ASC")
                        e.Item.OwnerTableView.Rebind()
                         Exit Select
                     Case GridSortOrder.Ascending
                        e.Item.OwnerTableView.DataSource = GetDataTable("SELECT FirstName, LastName FROM Employees ORDER BY LEN(FirstName) DESC")
                        e.Item.OwnerTableView.Rebind()
                         Exit Select
                     Case GridSortOrder.Descending
                        e.Item.OwnerTableView.DataSource = GetDataTable("SELECT FirstName, LastName FROM Employees")
                        e.Item.OwnerTableView.Rebind()
                         Exit Select
                 End Select
             ElseIf e.CommandArgument = "LastName" Then
                 Select Case e.OldSortOrder
                     Case GridSortOrder.None
                        e.Item.OwnerTableView.DataSource = GetDataTable("SELECT FirstName, LastName FROM Employees ORDER BY LastName DESC")
                        e.Item.OwnerTableView.Rebind()
                         Exit Select
                     Case GridSortOrder.Ascending
                        e.Item.OwnerTableView.DataSource = GetDataTable("SELECT FirstName, LastName FROM Employees ORDER BY LastName ASC")
                        e.Item.OwnerTableView.Rebind()
                         Exit Select
                     Case GridSortOrder.Descending
                        e.Item.OwnerTableView.DataSource = GetDataTable("SELECT FirstName, LastName FROM Employees")
                        e.Item.OwnerTableView.Rebind()
                         Exit Select
                 End Select
             End If
End Sub
Protected Sub RadGrid2_NeedDataSource(ByVal source As Object, ByVal e As GridNeedDataSourceEventArgs) Handles RadGrid2.NeedDataSource
            RadGrid2.DataSource = GetDataTable("SELECT FirstName, LastName FROM Employees")
End Sub