Back to Devexpress

ColumnBase.SortMode Property

wpf-devexpress-dot-xpf-dot-grid-dot-columnbase-85441615.md

latest6.3 KB
Original Source

ColumnBase.SortMode Property

Gets or sets how the column’s data is sorted when sorting is applied to it. This is a dependency property.

Namespace : DevExpress.Xpf.Grid

Assembly : DevExpress.Xpf.Grid.v25.2.Core.dll

NuGet Package : DevExpress.Wpf.Grid.Core

Declaration

csharp
[DefaultValue(ColumnSortMode.Default)]
public ColumnSortMode SortMode { get; set; }
vb
<DefaultValue(ColumnSortMode.Default)>
Public Property SortMode As ColumnSortMode

Property Value

TypeDefaultDescription
ColumnSortModeDefault

A ColumnSortMode enumeration value that specifies the sort mode.

|

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 specifies the algorithm used to sort the column’s data (by display text, edit value, or a custom sorting algorithm).

The Default option is DisplayText for columns that use in-place editors inherited from the LookUpEditSettingsBase class (ComboBoxEditSettings, LookUpEditSettings). The Default option is Value for other columns.

Follow the steps below to apply custom sort and group options:

  1. Specify the ColumnBase.SortOrder or GridColumn.GroupIndex property.
  2. Set the SortMode property to Custom.
  3. Handle the GridControl.CustomColumnSort or GridControl.CustomColumnGroup event.

Refer to the following help topics for more information: Sorting Modes and Custom Sorting, Group Modes and Custom Grouping.

Example

The following example demonstrates how to use custom rules to sort data in the GridControl:

View Example: How to Use Custom Rules to Sort Data

xaml
<dxg:GridControl x:Name="grid"
                 CustomColumnSort="OnCustomColumnSort">
    <dxg:GridControl.Columns>
        <dxg:GridColumn FieldName="Day" GroupIndex="0" SortMode="Custom"/>
        <dxg:GridColumn FieldName="Employee"/>
    </dxg:GridControl.Columns>
</dxg:GridControl>
csharp
void OnCustomColumnSort(object sender, CustomColumnSortEventArgs e) {
    if(e.Column.FieldName == "Day") {
        int dayIndex1 = GetDayIndex(e.Value1);
        int dayIndex2 = GetDayIndex(e.Value2);
        e.Result = dayIndex1.CompareTo(dayIndex2);
        e.Handled = true;
    }
}
int GetDayIndex(object day) {
    return (int)Enum.Parse(typeof(DayOfWeek), (string)day);
}
vb
Private Sub OnCustomColumnSort(ByVal sender As Object, ByVal e As DevExpress.Xpf.Grid.CustomColumnSortEventArgs)
    If Equals(e.Column.FieldName, "Day") Then
        Dim dayIndex1 As Integer = Me.GetDayIndex(e.Value1)
        Dim dayIndex2 As Integer = Me.GetDayIndex(e.Value2)
        e.Result = dayIndex1.CompareTo(dayIndex2)
        e.Handled = True
    End If
End Sub

Private Function GetDayIndex(ByVal day As Object) As Integer
    Return CInt(System.[Enum].Parse(GetType(System.DayOfWeek), CStr(day)))
End Function

The following code snippets (auto-collected from DevExpress Examples) contain references 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.

wpf-data-grid-implement-custom-grouping/CS/CustomGrouping_CodeBehind/MainWindow.xaml#L15

xml
<dxg:GridColumn FieldName="LastName" />
<dxg:GridColumn FieldName="UnitPrice" SortMode="Custom" GroupIndex="0">
    <dxg:GridColumn.EditSettings>

wpf-data-grid-implement-custom-sorting/CS/CustomSorting_CodeBehind/MainWindow.xaml#L11

xml
<dxg:GridControl.Columns>
    <dxg:GridColumn FieldName="Day" GroupIndex="0" SortMode="Custom"/>
    <dxg:GridColumn FieldName="Employee"/>

See Also

Group Modes and Custom Grouping

ColumnBase Class

ColumnBase Members

DevExpress.Xpf.Grid Namespace