Skip to main content
A newer version of this page is available. .

GridColumn.SortMode Property

Gets or sets how the column’s data are sorted/grouped.

Namespace: DevExpress.XtraGrid.Columns

Assembly: DevExpress.XtraGrid.v18.2.dll

Declaration

[DXCategory("Data")]
[DefaultValue(ColumnSortMode.Default)]
[XtraSerializableProperty]
[XtraSerializablePropertyId(2)]
public ColumnSortMode SortMode { get; set; }

Property Value

Type Default Description
ColumnSortMode **Default**

The sort mode for column data.

Available values:

Name Description
Default

Sorts the column’s data according to the type of the editor assigned to the column.

The Default option is equivalent to DisplayText for columns that use LookUpEdit, ImageComboBoxEdit and HypertextLabel (RepositoryItemHypertextLabel) in-place editors.

The Default option is equivalent to Value for other columns. Note that for certain editors (TextEdit, ComboBoxEdit, etc) the edit values match the display values.

Value

Sorts the column’s data by the column’s edit values (these are synchronized with the bound data source’s values).

DisplayText

Sorts the column’s data by the column’s display text (the strings displayed within the column’s cells).

Custom

Enables custom sorting of a column’s data. To implement custom sorting, handle the ColumnView.CustomColumnSort event in the GridControl, and the TreeList.CompareNodeValues event in the TreeList.

In the GridControl, the Custom mode also enables custom grouping of rows when grouping is applied against the current column. To implement custom grouping, handle the GridView.CustomColumnGroup event.

Remarks

The SortMode property determines the algorithm used to sort the column data (by display text, by edit value, or using a custom sort algorithm).

To provide custom sorting and/or grouping, set the SortMode property to ColumnSortMode.Custom and handle the ColumnView.CustomColumnSort and GridView.CustomColumnGroup events. See the ColumnSortMode topic for details on other available sorting modes.

To apply sorting to a column, the column’s GridColumn.SortOrder property can be set to ColumnSortOrder.Ascending or ColumnSortOrder.Descending. This will arrange the column values in ascending or descending order using the algorithm specified by the SortMode property.

Note

In server mode, don’t use the SortMode property to enable data sorting and grouping by display values. Use the GridColumn.FieldNameSortGroup property instead.

See the Custom sorting module demo which shows how to sort a string column with decimal values.


    // Custom Column Sorting            
    GridColumn column = gridView.Columns["Length"];
    column.SortMode = ColumnSortMode.Custom;
    gridView.CustomColumnSort += (sender, e) => {
        // Always show 0 at the bottom
        if (e.Column.FieldName == "Length") {
            if ((double)e.Value1 == 0 && (double)e.Value2 != 0) {
                e.Result = e.SortOrder == ColumnSortOrder.Ascending ? 1 : -1;
            }
            else if ((double)e.Value2 == 0 && (double)e.Value1 != 0) {
                e.Result = e.SortOrder == ColumnSortOrder.Ascending ? -1 : 1;
            }
            else {
                // default comparison
                e.Result = Comparer.Default.Compare(e.Value1, e.Value2);
            }
            e.Handled = true;
        }
    };
    column.SortOrder = ColumnSortOrder.Ascending;

The following code snippet (auto-collected from DevExpress Examples) contains a reference to the SortMode property.

Note

The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

See Also