How to hide the custom option in the filterbar dropdown in WinForms GridGroupingControl?
Hide the custom option
To achieve this, implement a custom filter bar cell that derives the cell model or cell renderer from the GridTableFilterBarCellModel or GridTableFilterBarCellRenderer.
The GridTableFilterBarCellModel.FillWithChoices method is overridden to remove the string Custom from the list.
C#
//Overrides FillWithChoices to remove the Custom option from the list. public override void FillWithChoices(ListBox listBox, GridStyleInfo style, out bool exclusive) { exclusive = true; GridTableCellStyleInfo tableStyleInfo = (GridTableCellStyleInfo)style; object[] items = (object[])GetFilterBarChoices(tableStyleInfo.TableCellIdentity); listBox.Items.Clear(); if(items != null) { listBox.Items.Add(SelectAllText); foreach (object item in items) { if(item is DBNull) listBox.Items.Add(SelectEmptyText); else if (item != null) listBox.Items.Add(style.GetFormattedText(item)); } } }
VB
'Overrides FillWithChoices to remove the Custom option from the list. Public Overrides Sub FillWithChoices(ByVal listBox As ListBox, ByVal style As GridStyleInfo, <System.Runtime.InteropServices.Out()> ByRef exclusive As Boolean) exclusive = True Dim tableStyleInfo As GridTableCellStyleInfo = CType(style, GridTableCellStyleInfo) Dim items() As Object = CType(GetFilterBarChoices(tableStyleInfo.TableCellIdentity), Object()) listBox.Items.Clear() If items IsNot Nothing Then listBox.Items.Add(SelectAllText) For Each item As Object In items If TypeOf item Is DBNull Then listBox.Items.Add(SelectEmptyText) ElseIf item IsNot Nothing Then listBox.Items.Add(style.GetFormattedText(item)) End If Next item End If End Sub
The GridTableFilterBarCellModel.Select method is rewritten with the Select_withoutcustom method that specifies the index for the selected item. You can pass the index for the Custom selected item as 0.
C#
//Customizes the Select method to hide the Custom option. public void Select_WithoutCustom(GridTableCellStyleInfoIdentity tableCellIdentity, int index) { if(index >= 0) { if(index == 0) { ResetFilterBar(tableCellIdentity); } else if (index == 1) { SelectItem(tableCellIdentity, 0); } else { SelectItem(tableCellIdentity, index - 1); } } }
VB
'Customizes the Select method to hide the Custom option. Public Sub Select_WithoutCustom(ByVal tableCellIdentity As GridTableCellStyleInfoIdentity, ByVal index As Integer) If index >= 0 Then If index = 0 Then ResetFilterBar(tableCellIdentity) ElseIf index = 1 Then SelectItem(tableCellIdentity, 0) Else SelectItem(tableCellIdentity, index - 1) End If End If End Sub
The GridTableFilterBarCellModel.ListBoxMouseUp() method is overridden to invoke the Select_withoutcustom method instead of the Select method.
C#
//Overrides the ListBoxMouseUP method to call the Customized Select method instead of the usual 'Select' method. protected override void ListBoxMouseUp(object sender, MouseEventArgs e) { CurrentCell.CloseDropDown(PopupCloseType.Done); GridTableCellStyleInfo tableStyleInfo = (GridTableCellStyleInfo)this.StyleInfo; GridTableCellStyleInfoIdentity tableCellIdentity = tableStyleInfo.TableCellIdentity; Model.Select_WithoutCustom(tableCellIdentity, this.ListBoxPart.SelectedIndex); SetTextBoxText(GetFilterBarText(StyleInfo), false);// don't call base class - ignore. String _FilterBarValue = this.ListBoxPart.SelectedItem.ToString(); GridTableControl _grid = Grid as GridTableControl; int _filed = _grid.TableDescriptor.ColIndexToField(ColIndex); string _columnName = _grid.TableDescriptor.VisibleColumns[_filed].Name; _grid.TableDescriptor.RecordFilters.Remove(_columnName); if(_FilterBarValue != "(All)" && _FilterBarValue != "Empty") _grid.TableDescriptor.RecordFilters.Add(_columnName, FilterCompareOperator.Equals, this.ListBoxPart.SelectedItem); }
VB
'Overrides the ListBoxMouseUP method to call the Customized Select method instead of the usual Select method. Protected Overrides Sub ListBoxMouseUp(ByVal sender As Object, ByVal e As MouseEventArgs) CurrentCell.CloseDropDown(PopupCloseType.Done) Dim tableStyleInfo As GridTableCellStyleInfo = CType(Me.StyleInfo, GridTableCellStyleInfo) Dim tableCellIdentity As GridTableCellStyleInfoIdentity = tableStyleInfo.TableCellIdentity Model.Select_WithoutCustom(tableCellIdentity, Me.ListBoxPart.SelectedIndex) SetTextBoxText(GetFilterBarText(StyleInfo), False) ' don't call base class - ignore. Dim _FilterBarValue As String = Me.ListBoxPart.SelectedItem.ToString() Dim _grid As GridTableControl = TryCast(Grid, GridTableControl) Dim _filed As Integer = _grid.TableDescriptor.ColIndexToField(ColIndex) Dim _columnName As String = _grid.TableDescriptor.VisibleColumns(_filed).Name _grid.TableDescriptor.RecordFilters.Remove(_columnName) If _FilterBarValue <> "(All)" AndAlso _FilterBarValue <> "Empty" Then _grid.TableDescriptor.RecordFilters.Add(_columnName, FilterCompareOperator.Equals, Me.ListBoxPart.SelectedItem) End If End Sub
A new CellType is created by adding the instance of the derived GridTableFilterBarCellModel class into the GridGroupingControl’s cellmodels.
C#
//Adds the GridTableFilterBarCell into the cell model this.gridGroupingControl1.TableModel.CellModels.Add("GridTableFilterBarCell", new MYGridTableFilterBarCellModel(this.gridGroupingControl1.TableModel)); //Assigns the FilterBarCelltype as GridTableFilterBarCell this.gridGroupingControl1.TableDescriptor.Appearance.FilterBarCell.CellType = "GridTableFilterBarCell"; this.gridGroupingControl1.TableDescriptor.VisibleColumns.Remove("Name");
VB
'Adds the GridTableFilterBarCell into the cell model Me.gridGroupingControl1.TableModel.CellModels.Add("GridTableFilterBarCell", New MYGridTableFilterBarCellModel(Me.gridGroupingControl1.TableModel)) 'Assigns the FilterBarCelltype as GridTableFilterBarCell Me.gridGroupingControl1.TableDescriptor.Appearance.FilterBarCell.CellType = "GridTableFilterBarCell" Me.gridGroupingControl1.TableDescriptor.VisibleColumns.Remove("Name")
After applying the properties, the Grid is displayed as follows.
Figure 1: Hiding the custom option
Samples: