Skip to main content

Sort Data in Code

  • 4 minutes to read

Allow Users to Sort Data

Use the following properties to control whether users can sort data by all columns or only specific columns:

Property

Description

View.OptionsCustomization.AllowSort

Allows or prevents users from sorting any column in a View.

GridColumn.OptionsColumn.AllowSort

Allows or prevents users from sorting by a specific column.

// Prevent users from sorting data by values in the 'OrderId' column
gridView.Columns["OrderId"].OptionsColumn.AllowSort = DevExpress.Utils.DefaultBoolean.False;

Note

You can sort data programmatically using the relevant APIs, even if AllowSort properties are disabled.

Sort Data and Change Sort Order

Use the following API to sort data or change the sort order:

API

Description

GridColumn.SortOrder

Sorts Grid data by the column. Set the SortOrder property to ColumnSortOrder.None to cancel sorting by the column.

GridColumn.SortIndex

Specifies the column’s position among sorted columns (in the ColumnView.SortedColumns collection). Set the SortIndex property to -1 to remove sorting by the column.

ColumnView.SortInfo

A collection that stores ColumnSortInfo objects for each grid column involved in sorting.

ColumnView.ClearSorting

Clears sorting.

If you execute multiple sort operations in code, wrap the corresponding code blocks in ColumnView.BeginSort/ColumnView.EndSort method calls to prevent excessive redraw operations.

The following code snippet clears sorting, then sorts data first by the Order Date column in ascending order, followed by the Customer ID column in descending order.

using DevExpress.Data;

gridView1.BeginSort();
gridView1.SortInfo.ClearAndAddRange(new[] {
  new GridColumnSortInfo(colOrderDate, ColumnSortOrder.Ascending), 
  new GridColumnSortInfo(colCustomerID, ColumnSortOrder.Descending)
});
gridView1.EndSort();

// You can also do the following:
// gridView1.ClearSorting();
// colOrderDate.SortOrder = ColumnSortOrder.Ascending;
// colCustomerID.SortOrder = ColumnSortOrder.Descending;

Start and End Sort Events

For each sort operation, the View does the following:

  1. Raises the StartSorting event before the actual sort operation takes place.

    Handle the StartSorting event to perform specific actions or custom logic before the data is sorted (for example, you can log which columns are being sorted). This event does not allow you to cancel a sort operation.

  2. Sorts data.
  3. Raises the EndSorting event.

Custom Sorting

Set the column’s SortMode property to ColumnSortMode.Custom. The View raises the CustomColumnSort event before it starts sorting data by values in this column. Handle the CustomColumnSort event to compare data rows and specify the order of rows.

Event parameters include:

e.Result
Set e.Result to 1 if the data row should appear first. Otherwise, set e.Result to -1.
e.Handled
Set e.Handled to true to cancel the default sorting behavior.

The following code snippet sorts data by values in the “colBirthDate” column in a custom order. It compares dates by months and days, ignoring the year:

using DevExpress.XtraGrid;

public Form1() {
  colBirthDate.SortMode = ColumnSortMode.Custom;
  colBirthDate.SortIndex = 0;
}
void gridView_CustomColumnSort(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnSortEventArgs e) {
  if (e.Column.FieldName != "BirthDate") return;
  e.Handled = true;
  int month1 = Convert.ToDateTime(e.Value1).Month;
  int month2 = Convert.ToDateTime(e.Value2).Month;
  if (month1 > month2)
    e.Result = 1;
  else if (month1 < month2)
    e.Result = -1;
  else
  e.Result = System.Collections.Comparer.Default.Compare(Convert.ToDateTime(e.Value1).Day, Convert.ToDateTime(e.Value2).Day);
}

DevExpress WinForms Data Grid custom sorting in code

Tip

The Data Grid can display non-sortable columns (for example, columns with images). Read the following help topic for more information: Custom Sorting and Non-Sortable Columns.

See Also