Gridview with an Enum Flags Column

3 Answers 20 Views
GridView
Stephan
Top achievements
Rank 3
Bronze
Iron
Iron
Stephan asked on 20 May 2025, 12:38 PM

Hello Telerik Support Team,

i found a solution for using enum values in a grid in the knowledge base:
https://docs.telerik.com/devtools/winforms/knowledge-base/gridview-comboboxcolumn-enum

I tried to adapt it for my purpose:

My enum and column definitions are:

[Flags]
public enum SpartenFlags
{
    [Display(Name = "Keine")]
    None = 0,
    Strom = 1,
    Gas = 2,
    Wasser = 4,
    Abwasser = 8,
    Breitband = 16,
    [Display(Name = "Fernwärme")]
    Fernwaerme = 32
}

gridViewComboBoxColumn2.EnableExpressionEditor = false;
gridViewComboBoxColumn2.FieldName = "Sparten";
gridViewComboBoxColumn2.HeaderText = "Sparten";
gridViewComboBoxColumn2.MinWidth = 25;
gridViewComboBoxColumn2.Name = "Sparten";
gridViewComboBoxColumn2.Width = 45;
gridViewComboBoxColumn2.DataSource = EnumWrapper<TBM_HAProzessPlugin.SpartenFlags>.EnumToList<TBM_HAProzessPlugin.SpartenFlags>();
gridViewComboBoxColumn2.DisplayMember = "Name";
gridViewComboBoxColumn2.ValueMember = "ID";

 

I override the view with a CustomGridImageCellElement (derived from GridDataCellElement) to display an icon depending on the combination of flag values. This works fine so far. But the filtering only works with single values. I would like to have a checkbox for each flag value and the filtering working correclty for every combination, that includes the checked values. Is there a way to archieve this?

 

Thank you for your help.

Best Regards,

Stephan

 

3 Answers, 1 is accepted

Sort by
0
Nadya | Tech Support Engineer
Telerik team
answered on 23 May 2025, 09:43 AM

Hello, Stephan,

Based on the information that you provided and your enum SpartenFlags, I created a sample project to include your enum class and display a GridViewComboBoxColumn in the grid that consumes it. According to your information, you have a a CustomGridImageCellElement (derived from GridDataCellElement) to display an icon but I am not aware of the implementation of your custom cell. 

You can find my project attached to this thread. Is it possible to modify it in a way to replicate your exact setup regarding the GridViewComboBoxColumn and the CustomGridImageCellElement, so that I can understand what is your exact setup is and your desired behavior. If I understand correctly, you need to have a checkbox for each value. Can you clarify what exactly you mean by "filtering working correctly for every combination"?

I am looking forward to your reply. 

Regards,
Nadya | Tech Support Engineer
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

0
Stephan
Top achievements
Rank 3
Bronze
Iron
Iron
answered on 23 May 2025, 01:41 PM

Hello Nadya,

thanks for your fast reply. I provided you a full solution based on your sample to replicate the scenario. I added a couple of rows with different values:

I want to filter the grid only based on the one "Sparten"-Column, which combines 6 bool combinations. But i don't want to add a checkbox column for each individual value. So the 6 values are combined to one Enum-Flags-Value.

 

I already solved the display of the values with a combination of icons, which works fine and is maybe interesting for others:

 

Regards,

Stephan

Nadya | Tech Support Engineer
Telerik team
commented on 28 May 2025, 12:02 PM

Hello, Stephan,

Thank you for the updated project that shows your custom implementation and the custom CustomGridImageCellElement.

If I understand your exact requirement correctly, you would like to filter by SpartenFlags and show all the rows that contain the selected flag.

To achieve the desired behavior, you can use the Custom Filtering mechanism that RadGridView offers. Custom filtering is a flexible mechanism for filtering RadGridView rows by using custom logic. In the CustomFiltering event, you can hide/show only the rows that you specify in your custom requirement. 

I modified the provided project to demonstrate how this approach can be used:

 private HashSet<string> selectedNodes = new HashSet<string>();
        private void RadGridView1_FilterPopupRequired(object sender, FilterPopupRequiredEventArgs e)
        {
            if (e.Column.Name == "Sparten" && e.FilterPopup is RadListFilterPopup popup)
            {
                popup.MenuTreeElement.TreeView.SelectedNodeChanged += TreeView_SelectedNodeChanged;
            }
        }
        private void TreeView_SelectedNodeChanged(object sender, RadTreeViewEventArgs e)
        {
            var node = e.Node;
            if (node.CheckState == Telerik.WinControls.Enumerations.ToggleState.On)
            {
                selectedNodes.Remove(node.Text);
            }
            if (node.CheckState == Telerik.WinControls.Enumerations.ToggleState.Off)
            {
                selectedNodes.Add(node.Text);
            }
        }

        private void radGridView1_CustomFiltering(object sender, GridViewCustomFilteringEventArgs e)
        {
            var cellValue = e.Row.Cells["Sparten"].Value?.ToString() ?? string.Empty;
            e.Visible = selectedNodes.Any(sn => cellValue.Contains(sn));
        }

I also attached the updated project. Feel free to customize further the custom filtering using the suggested approach.

I hope this helps. If you need further assistance please let me know.

0
Stephan
Top achievements
Rank 3
Bronze
Iron
Iron
answered on 05 Jun 2025, 11:06 AM

Hello Nadya,

the provided solution doesn't work well, the filter treeview elements get lost often like this:

 

 

And if i click sort right after starting the demo app, no rows are shown anymore:

 

And also it requires to disable the filterering row :

this.radGridView1.MasterTemplate.ShowHeaderCellButtons = true;
this.radGridView1.MasterTemplate.ShowFilteringRow = false;

 

The real application grid is much bigger, as seen in my first post. So disabling it may upset our customers, who maybe use it or are used to it.

 

Regards

Stephan

Nadya | Tech Support Engineer
Telerik team
commented on 10 Jun 2025, 09:42 AM

Hello,  Stephan,

I understand your considerations regarding disabling the filtering row. It would not be desired to change the current behavior of your users if they are used to it and already know how to use it.

However, I would like to note that filtering by image is not supported out of the box. In your CustomGridImageCellElement, you inherits from GridDataCellElement. GridDataCellElement allows filtering, but in the most common case, you can filter it by text. In your SpartenFlags enum contains an image and ID. Hence, the filter descriptors that are available in the filtering row should be relevant for filtering numeric value, and would appear such as "Greater than", "Less than", "Equals", etc. And this is expected. You can continue to use ShowFilteringRow to true, as before, but you will need to specify which filter exactly you want to see in the options and limit the grid to these filters.

RadGridView offers GridViewImageColumn and GridImageCellElement that are specially designed to display images, but they do not provide filtering functionality. To filter cells in the grid, you need to filter their values, images can not be filtered out of the box. The possible approach to filter a RadGridView by image is to implement custom filtering logic and use the CustomFiltering event, where you can show/hide rows according to any specific requirement that you define: Custom Filtering - WinForms GridView Control

The solution that I suggested to you was as close as possible to achieve the functionality that you needed. This is only possible by using a custom filtering approach and handling it according to your needs. Because, indeed, you filter the cells by text and show to the users only images (without any text). 

Now, I understand that it is not convenient for you as it breaks the existing behavior. Feel free to browse the internet and adopt any solution that might be useful for you in handling such cases. I am not familiar with your exact requirements, and we always base our answers on the information that you provided so far. In your case, using the custom filtering approach is the way to go.  However, it is up to the developer to define how exactly the rows should be filtered in the grid. 

I hope this information helps. Let me know if you have other questions.

 

Tags
GridView
Asked by
Stephan
Top achievements
Rank 3
Bronze
Iron
Iron
Answers by
Nadya | Tech Support Engineer
Telerik team
Stephan
Top achievements
Rank 3
Bronze
Iron
Iron
Share this question
or