Category / Section
How to remove the custom option from the filter dropdown in WinForms GridGroupingControl?
2 mins read
Remove the custom option from filter dropdown
To remove the “Custom” option in the filterbar dropdown list, you can use the TableControlCurrentCellShowingDropDown event. When the second item is removed, index conflict occurs. You can swap the selected index through the FilterBarSelectedItemChanging event to avoid such conflicts.
C#
this.gridGroupingControl1.TableControlCurrentCellShowingDropDown += gridGroupingControl1_TableControlCurrentCellShowingDropDown; this.gridGroupingControl1.FilterBarSelectedItemChanging += gridGroupingControl1_FilterBarSelectedItemChanging; //Avoids the index conflict problems in the SelectedIndex. void gridGroupingControl1_FilterBarSelectedItemChanging(object sender, FilterBarSelectedItemChangingEventArgs e) { if(e.SelectedIndex > 0) { e.SelectedIndex += 1; } } void gridGroupingControl1_TableControlCurrentCellShowingDropDown(object sender, GridTableControlCurrentCellShowingDropDownEventArgs e) { //Removes custom option from the filterbar when the EnableLegacyStyle is default. if(e.TableControl.CurrentCell.Renderer is GridTableFilterBarCellRenderer) { GridComboBoxCellRenderer renderer = e.TableControl.CurrentCell.Renderer as GridComboBoxCellRenderer; renderer.ListBoxPart.Items.RemoveAt(1); } //When the EnableLegacyStyle is set to false, the options of the filterbar executes improper functions. //Use the GridTableFilterBarGridListCellRenderer for avoiding these types of problems. if(e.TableControl.CurrentCell.Renderer is GridTableFilterBarGridListCellRenderer) { GridTableFilterBarGridListCellRenderer renderer = e.TableControl.CurrentCell.Renderer as GridTableFilterBarGridListCellRenderer; renderer.ListControlPart.Items.RemoveAt(1); } }
VB
Private Me.gridGroupingControl1.TableControlCurrentCellShowingDropDown += AddressOf gridGroupingControl1_TableControlCurrentCellShowingDropDown Private Me.gridGroupingControl1.FilterBarSelectedItemChanging += AddressOf gridGroupingControl1_FilterBarSelectedItemChanging 'Avoids the index conflict problems in the SelectedIndex. Private Sub gridGroupingControl1_FilterBarSelectedItemChanging(ByVal sender As Object, ByVal e As FilterBarSelectedItemChangingEventArgs) If e.SelectedIndex > 0 Then e.SelectedIndex += 1 End If End Sub Private Sub gridGroupingControl1_TableControlCurrentCellShowingDropDown(ByVal sender As Object, ByVal e As GridTableControlCurrentCellShowingDropDownEventArgs) 'Removes custom option from the filterbar when the EnableLegacyStyle is default. If TypeOf e.TableControl.CurrentCell.Renderer Is GridTableFilterBarCellRenderer Then Dim renderer As GridComboBoxCellRenderer = TryCast(e.TableControl.CurrentCell.Renderer, GridComboBoxCellRenderer) renderer.ListBoxPart.Items.RemoveAt(1) End If 'When the EnableLegacyStyle is set to false, the options of the filterbar executes improper functions. 'Uses the GridTableFilterBarGridListCellRenderer for avoiding these types of problems. If TypeOf e.TableControl.CurrentCell.Renderer Is GridTableFilterBarGridListCellRenderer Then Dim renderer As GridTableFilterBarGridListCellRenderer = TryCast(e.TableControl.CurrentCell.Renderer, GridTableFilterBarGridListCellRenderer) renderer.ListControlPart.Items.RemoveAt(1) End If End Sub
The following screenshot displays the Grid with the “Custom” option removed.
Figure 1: Grid without "Custom" option
Samples:
C#: Remove option
VB: Remove option