Skip to main content

Sorting

  • 3 minutes to read

This section provides information on sorting data in the Data Grid.

Watch Video

Sort Data

By default, end-users can sort data by any column, except for columns with MemoExEdit, ImageEdit and PictureEdit in-place editors. At runtime, click a column header once to sort data ascending. Consequent clicks will reverse the sort order. Sort glyph (up or down arrow) in column headers indicates the current sort order.

Grid - Sorting Animation

To sort data by a specific column end-users can also right-click this column’s header and select “Sort Ascending” or “Sort Descending”.

Grid - Sort in Menu

Related API

Multi-Column Sorting

The Data Grid discards the currently applied sorting when end-users click column headers, and keeps it when users sort data through header context menus. Thus, to sort data by multiple columns, end-users should either utilize header context menus or hold the Shift key down when clicking column headers. In the figure below, data is first sorted ascending by the “Customer ID” field and then descending by order dates.

Grid - Multi-Column Sorting

Clear Sorting

To remove data sorting by a specific column, end-users should click this column’s header with the Ctrl key pressed. Alternatively, a user can right-click this header and select “Clear Sorting”. To simultaneously remove sorting by all columns, select “Clear All Sorting” instead.

Grid - Unsort in Menu

Related API

  • GridOptionsCustomization.AllowSort - prevents end-users from unsorting data by Ctrl+clicking column headers and disables the “Clear Sorting” option for all grid columns.

  • OptionsColumn.AllowSort - prevents end-users from unsorting data by Ctrl+clicking this column’s header and disables the “Clear Sorting” option for this column.

  • GridView.PopupMenuShowing - allows you to manually modify or remove context menu items, with which end-users can change sorting. The code below changes the “Clear All Sorting” item behavior so that end-users will be unable to remove the predefined sorting by the “Order Date” column.

using System.Linq;
using DevExpress.Utils.Menu;
//. . .
gridView1.PopupMenuShowing += GridView1_PopupMenuShowing;
//. . .
private void GridView1_PopupMenuShowing(object sender, PopupMenuShowingEventArgs e) {
    DXMenuItem clearSorting = e.Menu.Items.First(x => x.Caption == "Clear All Sorting");
    clearSorting.Click += ClearSorting_Click;
}

private void ClearSorting_Click(object sender, EventArgs e) {
    gridView1.SortInfo.ClearAndAddRange(new[] {
            new GridColumnSortInfo(colOrderDate, DevExpress.Data.ColumnSortOrder.Ascending)
        });
}

Sort Groups by Summary Values

The Data Grid can sort groups according to these groups’ summary values. See the Summaries article for the details.

Data Grid - Summaries - Sorting by Summaries

Sort Modes

By default, columns with LookUpEdit and ImageComboBoxEdit in-place editors sort their data by editor’s display values. Other columns sort their data by the editor’s edit values. Use the GridColumn.SortMode property to change this sorting mode.

In the figure below, a column with ImageComboBoxEdit is forced to sort items by priority, from low to high. To do so, the GridColumn.SortMode must be changed to ColumnSortMode.Value. Otherwise, items are sorted alphabetically by their display values.

Grid - Sort Modes