Back to Devexpress

GridColumn.SortMode Property

windowsforms-devexpress-dot-xtragrid-dot-columns-dot-gridcolumn-306b5d8b.md

latest6.0 KB
Original Source

GridColumn.SortMode Property

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

Namespace : DevExpress.XtraGrid.Columns

Assembly : DevExpress.XtraGrid.v25.2.dll

NuGet Packages : DevExpress.Win.Grid, DevExpress.Win.Navigation

Declaration

csharp
[DefaultValue(ColumnSortMode.Default)]
[DXCategory("Data")]
[XtraSerializableProperty]
[XtraSerializablePropertyId(2)]
public ColumnSortMode SortMode { get; set; }
vb
<DXCategory("Data")>
<DefaultValue(ColumnSortMode.Default)>
<XtraSerializableProperty>
<XtraSerializablePropertyId(2)>
Public Property SortMode As ColumnSortMode

Property Value

TypeDefaultDescription
ColumnSortModeDefault

The sort mode for column data.

|

Available values:

NameDescription
Default

The actual sort mode is determined by a control. See the property description for more details.

| | 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 |

Applies sort options specified in the CustomColumnSort event handler.

In data grids, this mode also applies group options from the CustomColumnGroup event handler.

|

Remarks

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

The Default option is DisplayText for columns that use LookUpEdit and ImageComboBoxEdit in-place editors. The Default option is Value for other columns.

Set the SortMode property to ColumnSortMode.Custom and handle ColumnView.CustomColumnSort and GridView.CustomColumnGroup events to apply custom sort and group options.

Set the GridColumn.SortOrder property to ColumnSortOrder.Ascending or ColumnSortOrder.Descending to sort column data. As a result, the GridControl arranges column values in ascending or descending order based on the algorithm specified by the SortMode property.

Note

In server mode, do not 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.

csharp
// 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;
vb
' Custom Column Sorting            
    Dim column As GridColumn = gridView.Columns("Length")
    column.SortMode = ColumnSortMode.Custom
    AddHandler gridView.CustomColumnSort, Sub(sender, e)
        ' Always show 0 at the bottom
        If e.Column.FieldName = "Length" Then
            If CDbl(e.Value1) = 0 AndAlso CDbl(e.Value2) <> 0 Then
                e.Result = If(e.SortOrder = ColumnSortOrder.Ascending, 1, -1)
            ElseIf CDbl(e.Value2) = 0 AndAlso CDbl(e.Value1) <> 0 Then
                e.Result = If(e.SortOrder = ColumnSortOrder.Ascending, -1, 1)
            Else
                ' default comparison
                e.Result = Comparer.Default.Compare(e.Value1, e.Value2)
            End If
            e.Handled = True
        End If
    End Sub
    column.SortOrder = ColumnSortOrder.Ascending

See Also

SortOrder

CustomColumnGroup

CustomColumnSort

FieldNameSortGroup

Sort Data in Code

Summaries

GridColumn Class

GridColumn Members

DevExpress.XtraGrid.Columns Namespace