Grid - Custom Sorting

First RadGrid

Country CityCompany NameContact NameContact Title
 Page 1 of 10, items 1 to 10 of 91.
ArgentinaBuenos AiresCactus Comidas para llevarPatricio SimpsonSales Agent
ArgentinaBuenos AiresOcéano Atlántico Ltda.Yvonne MoncadaSales Agent
ArgentinaBuenos AiresRancho grandeSergio GutiérrezSales Representative
AustriaGrazErnst HandelRoland MendelSales Manager
AustriaSalzburgPiccolo und mehrGeorg PippsSales Manager
BelgiumBruxellesMaison DeweyCatherine DeweySales Agent
BelgiumCharleroiSuprêmes délicesPascale CartrainAccounting Manager
BrazilSao PauloComércio MineiroPedro AfonsoSales Associate
BrazilSao PauloFamilia ArquibaldoAria CruzMarketing Assistant
BrazilCampinasGourmet LanchonetesAndré FonsecaSales Associate

Demo Configurator

  • Select field name:Country
  • Select sort order:Ascending
  • True

Second RadGrid

First nameLast name
NancyDavolio
AndrewFuller
JanetLeverling
MargaretPeacock
StevenBuchanan
MichaelSuyama
RobertKing
LauraCallahan
AnneDodsworth
Isolate this demo as a stand-alone application
  • About this demo
  • C#
  • VB
This example demonstrates how the data can be sorted programmatically.
  • Using GridSortExpression class that provides sorting options for Telerik RadGrid
  • Using SortExpressions collection of GridTableView class
  • How to set the two different sorting modes - single or multi-column sorting of Telerik RadGrid
It also includes sorting persistence after postback operation.

The second grid on the demo allows you to apply custom sort criteria by setting the AllowCustomSorting property for the master 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.

Instructions

Demo instructions first grid:

  • Make your choice in the configuration section and click the button to apply / clear sorting.

Demo instructions second grid:

  • Click the "First Name" column to sort by string length
  • Click the "Last Name" column to sort descending on first click
  • DefaultCS.aspx
  • DefaultCS.aspx.cs
<%@ Page Language="c#" Inherits="Telerik.GridExamplesCSharp.Functionality.Sorting.CustomSorting.DefaultCS"CodeFile="DefaultCS.aspx.cs"  %>

<%@ Register TagPrefix="telerik" Namespace="Telerik.Web.UI" Assembly="Telerik.Web.UI" %>
<%@ Register TagPrefix="qsf" Namespace="Telerik.QuickStart" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns='http://www.w3.org/1999/xhtml'>
<head runat="server">
    <title>Telerik ASP.NET Example</title>
</head>
<body>
    <form id="form1" runat="server">
    <telerik:RadScriptManager runat="server" ID="RadScriptManager1" />
    <telerik:RadSkinManager ID="RadSkinManager1" runat="server" ShowChooser="true" />
    <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server" UpdateInitiatorPanelsOnly="true">
        <AjaxSettings>
            <telerik:AjaxSetting AjaxControlID="RadGrid1">
                <UpdatedControls>
                    <telerik:AjaxUpdatedControl ControlID="RadGrid1" LoadingPanelID="RadAjaxLoadingPanel1"></telerik:AjaxUpdatedControl>
                </UpdatedControls>
            </telerik:AjaxSetting>
            <telerik:AjaxSetting AjaxControlID="ConfigurationPanel1">
                <UpdatedControls>
                    <telerik:AjaxUpdatedControl ControlID="RadGrid1"></telerik:AjaxUpdatedControl>
                </UpdatedControls>
            </telerik:AjaxSetting>
            <telerik:AjaxSetting AjaxControlID="RadGrid2">
                <UpdatedControls>
                    <telerik:AjaxUpdatedControl ControlID="RadGrid2" LoadingPanelID="RadAjaxLoadingPanel1"></telerik:AjaxUpdatedControl>
                </UpdatedControls>
            </telerik:AjaxSetting>
        </AjaxSettings>
    </telerik:RadAjaxManager>
    <telerik:RadAjaxLoadingPanel ID="RadAjaxLoadingPanel1" runat="server">
    </telerik:RadAjaxLoadingPanel>

    <h2>First RadGrid</h2>
    <telerik:RadGrid RenderMode="Lightweight" ID="RadGrid1" DataSourceID="SqlDataSource1" runat="server"
        AllowSorting="True" AllowPaging="True" AutoGenerateColumns="False">
        <MasterTableView Width="100%" AllowMultiColumnSorting="true">
            <Columns>
                <telerik:GridBoundColumn SortExpression="Country" HeaderText="Country" HeaderButtonType="TextButton"
                    DataField="Country">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn SortExpression="City" HeaderText="City" HeaderButtonType="TextButton"
                    DataField="City">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn SortExpression="CompanyName" HeaderText="Company Name" HeaderButtonType="TextButton"
                    DataField="CompanyName">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn SortExpression="ContactName" HeaderText="Contact Name" HeaderButtonType="TextButton"
                    DataField="ContactName">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn SortExpression="ContactTitle" HeaderText="Contact Title"
                    HeaderButtonType="TextButton" DataField="ContactTitle">
                </telerik:GridBoundColumn>
            </Columns>
        </MasterTableView>
        <PagerStyle Mode="NumericPages"></PagerStyle>
    </telerik:RadGrid>
    <qsf:ConfiguratorPanel runat="server" ID="ConfigurationPanel1" Expanded="true">
        <Views>
            <qsf:View Title="Add/Remove sort expression">
                <qsf:ConfiguratorColumn ID="ConfiguratorColumn1" runat="server">
                    <ul class="fb-group">
                        <li>
                            <qsf:DropDownList ID="RadDropDownList3" runat="server" Width="120px" Label="Select field name:">
                                <Items>
                                    <telerik:DropDownListItem Value="Country" Text="Country"></telerik:DropDownListItem>
                                    <telerik:DropDownListItem Value="City" Text="City"></telerik:DropDownListItem>
                                    <telerik:DropDownListItem Value="CompanyName" Text="CompanyName"></telerik:DropDownListItem>
                                    <telerik:DropDownListItem Value="ContactName" Text="ContactName"></telerik:DropDownListItem>
                                    <telerik:DropDownListItem Value="ContactTitle" Text="ContactTitle"></telerik:DropDownListItem>
                                </Items>
                            </qsf:DropDownList>
                        </li>
                        <li>
                            <qsf:DropDownList ID="RadDropDownList2" runat="server" Width="120px" Label="Select sort order:">
                                <Items>
                                    <telerik:DropDownListItem Value="Ascending" Selected="True" Text="Ascending"></telerik:DropDownListItem>
                                    <telerik:DropDownListItem Value="Descending" Text="Descending"></telerik:DropDownListItem>
                                    <telerik:DropDownListItem Value="None" Text="No Sort"></telerik:DropDownListItem>
                                </Items>
                            </qsf:DropDownList>
                        </li>
                        <li>
                            <qsf:Button ID="btnAddExpression" runat="server" Text="Add" OnClick="btnAddExpression_Click"></qsf:Button>
                            <qsf:Button ID="Clear" runat="server" Text="Remove" OnClick="Clear_Click"></qsf:Button>
                        </li>
                    </ul>
                </qsf:ConfiguratorColumn>
            </qsf:View>
        </Views>
        <Views>
            <qsf:View Title="Allow multi-column sorting">
                <qsf:ConfiguratorColumn ID="ConfiguratorColumn2" runat="server">
                    <ul class="fb-group">
                        <li>
                            <qsf:DropDownList ID="RadDropDownList1" runat="server" AutoPostBack="True" Width="120px" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
                                <Items>
                                    <telerik:DropDownListItem Value="True" Selected="True" Text="True"></telerik:DropDownListItem>
                                    <telerik:DropDownListItem Value="False" Text="False"></telerik:DropDownListItem>
                                </Items>
                            </qsf:DropDownList>
                        </li>
                    </ul>
                </qsf:ConfiguratorColumn>
            </qsf:View>
        </Views>
    </qsf:ConfiguratorPanel>
    <hr />
    <h2>Second RadGrid</h2>
    <telerik:RadGrid RenderMode="Lightweight" ID="RadGrid2" runat="server" AllowSorting="True" AutoGenerateColumns="False"
        GridLines="None" OnSortCommand="RadGrid2_SortCommand" OnNeedDataSource="RadGrid2_NeedDataSource">
        <MasterTableView AllowCustomSorting="true">
            <Columns>
                <telerik:GridBoundColumn HeaderText="First name" HeaderButtonType="TextButton" DataField="FirstName">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn HeaderText="Last name" HeaderButtonType="TextButton" DataField="LastName">
                </telerik:GridBoundColumn>
            </Columns>
        </MasterTableView>
    </telerik:RadGrid>
    <asp:SqlDataSource ID="SqlDataSource1" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
        ProviderName="System.Data.SqlClient" SelectCommand="SELECT * FROM Customers"
        runat="server"></asp:SqlDataSource>
    </form>
</body>
</html>

Find Assistance

Help Us Improve

Was this example helpful?