Troubleshooting: Sort and Filter Operations
- 5 minutes to read
The sections below outline the most frequent issues with sort and filter operations in the GridControl.
Sorting and Filtering Do Not Work
If sorting or filtering do not work in your GridControl, check the following possible causes.
GridColumn Is Bound To a Complex Object
Problem
The GridControl can sort and filter types that support comparison operators. This means that most types you will display in grid cells are supported. A few examples are int
, string
, DateTime
, and decimal
.
On the other hand, the GridControl cannot compare complex or custom types (such as MyClass
), unless you override comparison operators for this type.
Solution
Use one of the following solutions:
Bind the column to your complex type’s nested property.
Use a dot in ColumnBase.FieldName to specify a complex path to a single nested property:
<dxg:GridColumn FieldName="Customer.Name"/>
Use Bands to display multiple nested properties under one header. If you want to customize the text displayed in cells, use the approaches described in the following topic: Format Cell Values.
If you do not want to bind columns to a nested property, implement the IComparable interface and ToString() method.
GridColumn Is Bound to a Collection
Problem
The GridControl cannot compare collections. Sorting is disabled for columns bound to collection types.
Solution
Ensure that columns are bound to data source fields. Refer to the following topic for more information: Binding Columns to Data Source Fields.
Use one of the following solutions:
Enable sorting and filtering by display text. Set the ColumnBase.ColumnFilterMode and ColumnBase.SortMode properties to
DisplayText
. To display a collection in a cell, set the target GridColumn’s EditSettings to ComboBoxEditSettings with CheckedComboBoxStyleSettings.If you enabled a filter by display text, the Auto Filter Row can work with your collection-bound column. Use the Contains condition to filter values by separate words.
Use the GridControl.SubstituteFilter event to update the grid filter. Refer to the following example for more information: Data Grid for WPF - Filter Columns Bound to Collection Properties.
GridColumn.Binding Is Used and the First Row Has Null in the Bound Property
Problem
If a column uses the ColumnBase.Binding property to obtain data, the GridControl checks the first row to determine the property type. If the field value in the first row is null
, the GridControl cannot generate a property descriptor.
A similar issue may occur if a column uses the ColumnBase.FieldName property and the bound property type is object
.
Solution
To let the GridControl know about the actual column type, set the ColumnBase.UnboundDataType property. If you use the ColumnBase.FieldName property to bind a column, use a binding instead and then set UnboundDataType
.
xmlns:sys="clr-namespace:System;assembly=mscorlib"
<dxg:GridColumn Binding="{Binding SomeProperty, Mode=TwoWay}" UnboundDataType="{x:Type sys:String}"/>
Server Mode Is Used with Unbound Columns
Problem
Unbound columns with values populated by GridControl.CustomUnboundColumnData or GridControl.CustomUnboundColumnDataCommand cannot be sorted or filtered in Server Mode.
Solution
Use the ColumnBase.UnboundExpression property to populate your columns.
Note
Columns populated with the ColumnBase.Binding property are processed as unbound because a binding can contain a custom converter. Refer to the following help topic for more information: Server Mode Limitations.
Virtual Sources Are Used
Problem
The sorting and filtering do not work if you use virtual sources.
Solution
To enable sorting and filtering in virtual sources, follow the steps described in the help topics below:
Numeric Values Are Sorted/Filtered as Strings
Problem
The GridControl sorts and filters numeric values as strings. This behavior occurs because ColumnBase.ColumnFilterMode is set to DisplayText
.
Solution
Set ColumnBase.ColumnFilterMode to Value
.
Duplicate Values Are Displayed in the Filter Popup
Problem
The GridControl displays filter values according to the information sent by EditSettings (Mask, DisplayFormat, DisplayTextConverter) and the CustomColumnDisplayText event. If multiple cell values have similar display text, the GridControl displays these values multiple times in the filter popup.
Solution
Use one of the following solutions:
- Set ColumnBase.ColumnFilterMode to
DisplayText
. - Change your display text settings to avoid duplicate text.
You may see duplicate values in the drop-down filter if you use DisplayFormat to round decimal values. To avoid this issue, we recommend that you round these values before the GridControl obtains them. You can use one of the following solutions:
Use a custom converter that rounds values in this Binding definition:
<dxg:GridColumn Header="Growth" Binding="{Binding Growth, Converter={StaticResource RoundConverter}, Mode=TwoWay}"/>
You can create your own custom converter or use the custom converter from this example.
Use unbound expressions:
<dxg:GridColumn Header="Growth" FieldName="GrowthUnbound" UnboundType="Decimal" UnboundExpression="Round([Growth], 2)" ReadOnly="True"/>
Use CustomUnboundColumnData (code behind) or CustomUnboundColumnDataCommand (MVVM):
<Grid> <dxg:GridControl ItemsSource="{Binding Source}" CustomUnboundColumnDataCommand="{Binding UnboundColumnDataCommand}"> <dxg:GridControl.Columns> <dxg:GridColumn FieldName="Name"/> <dxg:GridColumn Header="Growth" FieldName="GrowthUnbound" UnboundType="Decimal"/> </dxg:GridControl.Columns> </dxg:GridControl> </Grid>
Refer to the following GitHub example for more information: WPF Data Grid - How To Round Decimal Values In Grid Columns.
GridControl Does Not Sort/Filter Values According to Text Displayed in Cells
Problem
The issue occurs when you use a custom CellTemplate, CellDisplayTemplate, or CellEditTemplate. These templates do not affect sorting and filtering.
Solution
To change cell display text, use EditSettings (Mask, DisplayFormat, DisplayTextConverter) or the CustomColumnDisplayText event. To sort and filter by display text, set the ColumnBase.ColumnFilterMode and ColumnBase.SortMode to DisplayText
. Refer to the following help topic for more information: Format Cell Values.
Filtering/Sorting Does Not Refresh After an Update in the Data Source
Problem
The GridControl does not refresh filtering, sorting, and grouping when item properties are updated at the data source level.
Solution
To reapply sorting/filtering/grouping automatically, enable the GridControl.AllowLiveDataShaping option.
The CustomColumnSort Event Is Not Raised
Problem
The event may not fire due to the following reasons:
- The ColumnBase.SortMode property is not set to
Custom
. - You use GridControl with TreeListView and handle the GridControl.CustomColumnSort event.
Solution
- Set the ColumnBase.SortMode property to
Custom
. - Use the TreeListView event for custom sorting: TreeListView.CustomColumnSort.