Back to Devexpress

Sort Data

aspnet-3714-components-grid-view-concepts-sort-data.md

latest8.5 KB
Original Source

Sort Data

  • Aug 22, 2025
  • 4 minutes to read

ASPxGridView allows users to sort column data.

Run Demo: ASPxGridView - Sorting DataRun Demo: ASPxGridView - Sort by Summaries

Use the following properties to specify whether users can sort column data:

ASPxGridBehaviorSettings.AllowSortControls sort availability at the control level.GridDataColumnSettings.AllowSortControls sort availability at the column level. If this property value is Default, the column’s behavior depends on the ASPxGridBehaviorSettings.AllowSort property value.

aspx
<dx:ASPxGridView ID="grid" runat="server" AutoGenerateColumns="false">
    <SettingsBehavior AllowSort="true"/>
    <Columns>
        <dx:GridViewDataColumn FieldName="ContactName" SortIndex="2" SortOrder="Ascending" >
            <Settings AllowSort="false" SortMode="DisplayText" />
        </dx:GridViewDataColumn>
        <%--...--%>
    </Columns>
</dx:ASPxGridView>

The integrated data sort algorithm only works on types that implement the IComparable interface. If a column’s data type does not support this interface, or you wish to change the default sort order, apply a custom sort algorithm.

Sort in the UI

Click an unsorted column’s header to sort the column and clear the sort order for other columns. The sort glyph indicates the column’s current sort order. To change the sort order, click the header again.

Hold the Shift key and click the required column headers to sort data by multiple columns. To clear the sort order for an individual column, press the Ctrl key and click the column’s header.

The image below shows sorted columns in the grid:

  • The Country column’s data is sorted in descending order.
  • The City column’s data is sorted in ascending order.

Sort in Code

Sort a Column

Use any of the following API members to sort a column:

Clear Sorting

Use any of the following API members to clear the sort order:

API memberTypeDescription
ColumnSortingEventFires before the grid sorts its data by column values.
BeforeColumnSortingGroupingEventFires before a column is sorted or grouped.
CustomColumnSortEventAllows you to apply a custom sort algorithm to a column.
IsAllowSort(GridViewColumn)MethodIndicates whether sorting by the specified column is enabled.
GetSortedColumns()MethodReturns a collection of sorted columns.
SortCountPropertyGets the number of sorted columns (rows for ASPxVerticalGrid).
SortIndexPropertySpecifies the column’s sort priority among columns (a lower number indicates a higher priority).
SortOrderPropertySpecifies the column’s sort order.

Custom Sorting

  1. Set the SortMode property to Custom.
  2. Handle the CustomColumnSort event to apply a custom sort algorithm to the column.

The example below sorts the Country column by character length in descending order.

aspx
<dx:ASPxGridView ID="ASPxGridView1" runat="server" AutoGenerateColumns="false"
    OnCustomColumnSort="grid_CustomColumnSort">
    <Columns>
        <dx:GridViewDataColumn FieldName="Country" SortOrder="Descending">
            <Settings SortMode="Custom" />
        </dx:GridViewDataColumn>
        <%--...--%>
    </Columns>
</dx:ASPxGridView>
csharp
protected void grid_CustomColumnSort(object sender, DevExpress.Web.CustomColumnSortEventArgs e) {
    if(e.Column.FieldName == "Country") {
        e.Handled = true;
        string s1 = e.Value1.ToString(), s2 = e.Value2.ToString();
        if(s1.Length > s2.Length)
            e.Result = 1;
        else
            if(s1.Length == s2.Length)
            e.Result = Comparer.Default.Compare(s1, s2);
        else
            e.Result = -1;
    }
}
vb
Protected Sub grid_CustomColumnSort(ByVal sender As Object, ByVal e As DevExpress.Web.CustomColumnSortEventArgs)
    If e.Column.FieldName = "Country" Then
        e.Handled = True
        Dim s1 As String = e.Value1.ToString(), s2 As String = e.Value2.ToString()

        If s1.Length > s2.Length Then
            e.Result = 1
        ElseIf s1.Length = s2.Length Then
            e.Result = Comparer.[Default].Compare(s1, s2)
        Else
            e.Result = -1
        End If
    End If
End Sub

Sort Groups by Summary Values

To sort groups by their summary values, create an instance of the ASPxGroupSummarySortInfo object, customize its settings, and add this object to the GroupSummarySortInfo collection. Remove this object from the collection to disable the summary sort feature.

The example below sorts group rows by their summary values in descending order.

aspx
<dx:ASPxGridView ID="grid" runat="server" AutoGenerateColumns="False" KeyFieldName="ProductID">
    <Columns>
        <dx:GridViewDataTextColumn FieldName="ProductID" VisibleIndex="0" />
        <dx:GridViewDataTextColumn FieldName="CategoryName" VisibleIndex="1" GroupIndex="0" />
        <dx:GridViewDataTextColumn FieldName="ProductName" VisibleIndex="2" />
    </Columns>
    <GroupSummary>
        <dx:ASPxSummaryItem FieldName="ProductName" SummaryType="Count" />
    </GroupSummary>
</dx:ASPxGridView>
csharp
protected void Page_Load(object sender, EventArgs e) {
    grid.GroupSummarySortInfo.Clear();
    ASPxGroupSummarySortInfo sortInfo = new ASPxGroupSummarySortInfo();
    sortInfo.SortOrder = ColumnSortOrder.Descending;
    sortInfo.SummaryItem = grid.GroupSummary["ProductName", SummaryItemType.Count];
    sortInfo.GroupColumn = "CategoryName";
    grid.GroupSummarySortInfo.AddRange(sortInfo);
}
vb
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
    grid.GroupSummarySortInfo.Clear()
    Dim sortInfo As ASPxGroupSummarySortInfo = New ASPxGroupSummarySortInfo()
    sortInfo.SortOrder = ColumnSortOrder.Descending
    sortInfo.SummaryItem = grid.GroupSummary("ProductName", SummaryItemType.Count)
    sortInfo.GroupColumn = "CategoryName"
    grid.GroupSummarySortInfo.AddRange(sortInfo)
End Sub