This is a migrated thread and some comments may be shown as answers.

Excel-like filtering popup and Enum

5 Answers 233 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Marco
Top achievements
Rank 2
Veteran
Marco asked on 13 Feb 2014, 11:18 AM
Hello Telerik

I have a question about the customizing the filter popup of the RadGridView.

The RadGridView is displaying a read-only list of document which have a type (integer), a name (string), a date (datetime) and a filepath (string). There is also an Enum which define the legal value for the document type (and store a string description). I use CellFormating event to convert the Integer value of the document type to the corresponding string description.

How could I change the Nodes in the Treeview of the Excel-like filtering popup to display the string description of the value ? (we should keep the integer value for the filterdescriptor of course)

The second one is a little bonus for me

Is it possible to build the treeview from the Enum instead of the default behavior ? (Having a node for each value in the enum and not only the value currently in the datasource of the gridview. Theses extra nodes could be displayed but greyed out).

Thanks for reading.

Marco Guignard

5 Answers, 1 is accepted

Sort by
0
Accepted
Dess | Tech Support Engineer, Principal
Telerik team
answered on 18 Feb 2014, 09:20 AM
Hello Marco,

Thank you for contacting us.

You can customize the filter popup via the RadGridView.FilterPopupInitialized event and the RadListFilterPopup.PopupOpened event. Below is a sample code snippet, demonstrating how to replace the integer values for the 'Type' column with the converted enum value and display this value in the popup. As to the question about having tree node for each enum value in the filter popup, you can get all distinct values for the column and add the missing values.
public Form1()
{
    InitializeComponent();
 
    List<Document> docList = new List<Document>();
    Random rand = new Random();
    for (int i = 0; i < 10; i++)
    {
        docList.Add(new Document(rand.Next(1, 4),"Name" + i,
            DateTime.Now.AddDays(i),"filePath" + i));
    }
 
    this.radGridView1.DataSource = docList;
 
    this.radGridView1.ReadOnly = true;
}
 
private void Form1_Load(object sender, EventArgs e)
{
    this.radGridView1.EnableFiltering = true;
    this.radGridView1.MasterTemplate.ShowHeaderCellButtons = true;
    this.radGridView1.MasterTemplate.ShowFilteringRow = false;
}
 
public class Document
{
    public int Type { get; set; }
 
    public string Name { get; set; }
 
    public DateTime Date { get; set; }
 
    public string FilePath { get; set; }
 
    public Document(int type, string name, DateTime date, string filePath)
    {
        this.Type = type;
        this.Name = name;
        this.Date = date;
        this.FilePath = filePath;
    }
}
 
enum DocType
{
    Type1 = 1,
    Type2,
    Type3,
    Type4,
    Type5
}
 
private void radGridView1_CellFormatting(object sender,
    CellFormattingEventArgs e)
{
    if (e.Column.Name == "Type" && e.CellElement.Value != null)
    {
        e.CellElement.Text = ((DocType)e.CellElement.Value).ToString();
    }
}
 
GridViewColumnValuesCollection distinctValues;
 
private void radGridView1_FilterPopupInitialized(object sender,
    FilterPopupInitializedEventArgs e)
{
    distinctValues = e.Column.DistinctValues;
     
    RadListFilterPopup popup = e.FilterPopup as RadListFilterPopup;
    if (popup != null && e.Column.Name == "Type")
    {
        popup.PopupOpened -= popup_PopupOpened;
        popup.PopupOpened += popup_PopupOpened;
    }
}
 
private void popup_PopupOpened(object sender, EventArgs args)
{
    RadListFilterPopup popup = sender as RadListFilterPopup;
    if (popup != null)
    {
        Array enumValues = Enum.GetValues(typeof(DocType));
 
        RadTreeNode root = popup.MenuTreeElement.TreeView.Nodes[2];
        if (root.Text == "All" && root.Nodes.Count > 0)
        {
            foreach (RadTreeNode childNode in root.Nodes)
            {
                childNode.Value = childNode.Text;
                int nodeValue = int.Parse(childNode.Value.ToString());
                childNode.Text = ((DocType)nodeValue).ToString();
            }
        }
 
        foreach (DocType enumValue in enumValues)
        {
            if (!distinctValues.Contains((int)enumValue))
            {
                RadTreeNode additionalNode = new RadTreeNode();
                additionalNode.Value = (int)enumValue;
 
                additionalNode.Text = enumValue.ToString();
                additionalNode.Enabled = false;
                root.Nodes.Add(additionalNode);
            }
        }
    }
}

Note that this is just a sample approach, which demonstrates the idea and it may not cover all possible cases. Feel free to modify it on a way, which suits your scenario best.

I hope this information helps. Should you have further questions, I would be glad to help.

Regards,
Desislava
Telerik

Check out the new Telerik Platform - the only modular platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native apps. Register for the free online keynote and webinar to learn more about the Platform on Wednesday, February 12, 2014 at 11:00 a.m. ET (8:00 a.m. PT).

0
Marco
Top achievements
Rank 2
Veteran
answered on 19 Feb 2014, 03:19 PM
Hello Desislava,

Thank you very much for your very well documented answer (you could copy paste your post in the documentation), it's working pretty nice.

There is only one little issue.

If you have two node with the same text value (after the threenode manipulation), you will get in trouble when the final user will start playing with it. (First because your filter is a nonsense then because the app will crash)

I'm not concerned but someone else could run into it (testing the code with fake value, localization issue, ...).
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 24 Feb 2014, 11:09 AM
Hello Marco,

Thank you for writing back.

I am glad that the provided answer is suitable for your case. However, the RadTreeNodes in the sample code snippet are generated from the enum values and it is not allowed to have two identical definitions. Note that the Excel-like filtering popup is intended to contain only the available values from the grid rows and it is not reasonable to have additional nodes by which you are not allowed to filter.

Please do not hesitate to contact us if you have any additional questions.

Regards,
Desislava
Telerik
0
Marco
Top achievements
Rank 2
Veteran
answered on 24 Feb 2014, 12:48 PM
Hello Desislava,

The additional nodes are just here to say "There is no document of this type in the list! Don't look for them".

You are right with enum which havn't two identical definitions. In my case I'm using an attribute description of the enum to store the text value (it's more flexible because it's a string and you don't have coding constraint) but these attribute have no constraint about unicity (It could append especially when you have globalization and you leave something like "missing traduction").

Of course that's a filter list with identical définitions (text or value) is not usable.

I'm just giving a warning about this for other people which may look at this post and use their own routine for building the filter.
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 27 Feb 2014, 11:15 AM
Hello Marco,

Thank you for getting back to me.

I completely understand your point. However, it is a custom implementation for the RadListFilterPopup and all these possible specific cases should be handled by the developer according to his requirement. The nodes in the tree view should be distinguishable and it is up to you how to handle all of the specific scenarios, which are results from the custom logic.

Should you have further questions, I would be glad to help.

Regards,
Desislava
Telerik
Tags
GridView
Asked by
Marco
Top achievements
Rank 2
Veteran
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Marco
Top achievements
Rank 2
Veteran
Share this question
or