|
| 1 | +--- |
| 2 | +title: Customizing Filter Text for Enum and Bool Columns in RadGridView |
| 3 | +description: Learn how to customize the filter menu text for bool and enum columns in RadGridView for WinForms, replacing default values with user-friendly labels. |
| 4 | +type: how-to |
| 5 | +page_title: How to Change Filter Menu Text for Bool and Enum Columns in RadGridView |
| 6 | +meta_title: Changing Filter Menu Text for Bool and Enum Columns in RadGridView |
| 7 | +slug: customizing-filter-text-radgridview-winforms |
| 8 | +tags: radgridview, winforms, filter, bool, enum, customization |
| 9 | +res_type: kb |
| 10 | +ticketid: 1690734 |
| 11 | +--- |
| 12 | + |
| 13 | +## Environment |
| 14 | + |
| 15 | +|Product Version|Product|Author| |
| 16 | +|----|----|----| |
| 17 | +|2025.2.520|RadGridView for WinForms|[Dinko Krastev](https://www.telerik.com/blogs/author/dinko-krastev)| |
| 18 | + |
| 19 | +## Description |
| 20 | + |
| 21 | +I want to customize the automatic filter menu in RadGridView for WinForms in two ways: |
| 22 | + |
| 23 | +1. For columns of type `bool`, replace "True" and "False" with "Checked" and "Unchecked". |
| 24 | +2. For columns of type `enum`, display the text from their `[Description]` attribute instead of the raw enum names. |
| 25 | + |
| 26 | +## Solution |
| 27 | + |
| 28 | +To achieve this customization, handle the `FilterPopupInitialized` event of RadGridView and modify the text in the filter menu using the `RadTreeView.NodeFormatting` event. Below is the implementation: |
| 29 | + |
| 30 | +### Customizing Bool Column Filter Text |
| 31 | + |
| 32 | +Use the following code: |
| 33 | + |
| 34 | +````C# |
| 35 | + |
| 36 | +this.radGridView1.FilterPopupInitialized += RadGridView1_FilterPopupInitialized; |
| 37 | + |
| 38 | +private void RadGridView1_FilterPopupInitialized(object sender, FilterPopupInitializedEventArgs e) |
| 39 | +{ |
| 40 | + if (e.Column.Name == "boolColumn") // Replace with your column name |
| 41 | + { |
| 42 | + RadListFilterPopup radListFilterPopup = e.FilterPopup as RadListFilterPopup; |
| 43 | + |
| 44 | + if (radListFilterPopup != null) |
| 45 | + { |
| 46 | + radListFilterPopup.MenuTreeElement.TreeView.NodeFormatting -= BoolFormattingHandler; |
| 47 | + radListFilterPopup.MenuTreeElement.TreeView.NodeFormatting += BoolFormattingHandler; |
| 48 | + radListFilterPopup.MenuTreeElement.TreeView.Refresh(); |
| 49 | + } |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +private void BoolFormattingHandler(object sender, TreeNodeFormattingEventArgs e) |
| 54 | +{ |
| 55 | + if (e.Node.Text == "False") |
| 56 | + e.NodeElement.ContentElement.Text = "Unchecked"; |
| 57 | + else if (e.Node.Text == "True") |
| 58 | + e.NodeElement.ContentElement.Text = "Checked"; |
| 59 | +} |
| 60 | + |
| 61 | +```` |
| 62 | + |
| 63 | +### Customizing Enum Column Filter Text |
| 64 | + |
| 65 | +For enum values with `[Description]` attributes, use the following code: |
| 66 | + |
| 67 | +````C# |
| 68 | + |
| 69 | +this.radGridView1.FilterPopupInitialized += RadGridView1_FilterPopupInitialized; |
| 70 | + |
| 71 | +private void RadGridView1_FilterPopupInitialized(object sender, FilterPopupInitializedEventArgs e) |
| 72 | +{ |
| 73 | + if (e.Column.Name == "enumColumn") // Replace with your column name |
| 74 | + { |
| 75 | + RadListFilterPopup radListFilterPopup = e.FilterPopup as RadListFilterPopup; |
| 76 | + |
| 77 | + if (radListFilterPopup != null) |
| 78 | + { |
| 79 | + radListFilterPopup.MenuTreeElement.TreeView.NodeFormatting -= EnumFormattingHandler; |
| 80 | + radListFilterPopup.MenuTreeElement.TreeView.NodeFormatting += EnumFormattingHandler; |
| 81 | + radListFilterPopup.MenuTreeElement.TreeView.Refresh(); |
| 82 | + } |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +private void EnumFormattingHandler(object sender, TreeNodeFormattingEventArgs e) |
| 87 | +{ |
| 88 | + string description = GetEnumDescription(e.Node.Text); |
| 89 | + if (!string.IsNullOrEmpty(description)) |
| 90 | + e.NodeElement.ContentElement.Text = description; |
| 91 | +} |
| 92 | + |
| 93 | +private string GetEnumDescription(string enumName) |
| 94 | +{ |
| 95 | + // Replace YourEnumType with the actual enum type |
| 96 | + var fieldInfo = typeof(YourEnumType).GetField(enumName); |
| 97 | + var attributes = (DescriptionAttribute[])fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false); |
| 98 | + return attributes.Length > 0 ? attributes[0].Description : enumName; |
| 99 | +} |
| 100 | + |
| 101 | +```` |
| 102 | + |
| 103 | +## See Also |
| 104 | + |
| 105 | +- [RadGridView Documentation](https://docs.telerik.com/devtools/winforms/controls/gridview/overview) |
0 commit comments