Permanent editor in a filter cell
Environment
Product Version | 2018.1 220 |
Product | RadGridView for WinForms |
Description
By design, RadGridView uses editing mechanism. Thus, only one cell can be edited at a moment. A common scenario is to display a permanent editor in the filter cells in order to indicate better to the user that there are several options to choose.
Solution
At the end of this solution the illustrated result in the following video will be achieved:
Follow the detailed steps in order to create a custom filter cell and embed a permanent RadDropDownListElement to manage filtering.
1. Create a derivative of GridFilterCellElement.
In its CreateChildElements method you can add a RadDropDownListElement which holds the distinct column values, a LightVisualElement which displays the filter operator and a StackLayoutPanel which is the host container.
Subscribe to the RadDropDownListElement.SelectedIndexChanged event where the respective FilterDescriptor will be added/deleted according to the currently selected drop down item.
Override the SetContentCore method where to bind the RadDropDownList and synchronize its selection. Since RadGridView uses data virtualization and cell elements are being reused during operations like scrolling, the RowInfo.Tag property will store the currently selected index.
In the UpdateFilterButtonVisibility method the default filter button is collapsed.
The IsCompatible method indicates with which columns and data rows the custom cell element is compatible during the cells reuse. Since this filter cell will be used only for e specific column and the filtering row, the returned Boolean result is true only in this case.
Custom GridFilterCellElement
2. Create a custom column which will use the newly implemented filter cell element.
Create a derivative of GridViewTextBoxColumn.
Override its GetCellType method. Thus, you can return the type of the custom filter cell for the GridViewFilteringRowInfo.
Custom column
3. Next, add the custom column to RadGridView and fill the grid with data:
Fill data and add the custom column
4. The last thing we need to do is to prevent entering edit mode for the filter cell since there is a permanent editor now. For this purpose, it is necessary to cancel the CellBeginEdit event:
Fill data and add the custom column
A complete solution providing a C# and VB.NET project is available here.