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

Set filter on enum field does not work

8 Answers 338 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Markus
Top achievements
Rank 1
Markus asked on 28 Apr 2010, 04:24 PM
Hello, I have a data grid with a GridViewComboBoxColumn bound to an field which is an enumeration. I use DataSource and properties 'ValueMember' and 'DisplayMember' to map enum values to readable strings. When I try to set a filter on the column an exception ist thrown 'column <x> not found'  ( where x is the string represantation of the enum value )

Here is an example: If I then try to set filter to EProductType.Hard => EvaluateExcetion is thrown 'column [Hard] not found'
First FilterConditionType is 'NoFilter', when I select condition exception occurs

public enum EProductType 
    { 
        None = 0
        Soft = 1
        Hard = 2 
    } 
         
    class Product 
    { 
 
        EProductType _productType; 
 
        public EProductType ProductType 
        { 
            get { return _productType; } 
            set { _productType = value; } 
        } 
 
        string _key; 
 
        public string Key 
        { 
            get { return _key; } 
            set { _key = value; } 
        } 
 
        string _name; 
 
        public string Name 
        { 
            get { return _name; } 
            set { _name = value; } 
        } 
    } 
 
private void AddMapping() 
IList<ValueMapping> mappings = new List<ValueMapping>(); 
            mappings.Add(new ValueMapping(EProductType.None, "")); 
            mappings.Add(new ValueMapping(EProductType.Hard, "Hardware")); 
            mappings.Add(new ValueMapping(EProductType.Soft, "Software")); 
 
            Telerik.WinControls.UI.GridViewComboBoxColumn comboColumn = radGridView1.Columns[0] as Telerik.WinControls.UI.GridViewComboBoxColumn; 
            comboColumn.DataSource = mappings
            comboColumn.ValueMember = "ValueMember"
            comboColumn.DisplayMember = "DisplayMember"
 
class ValueMapping 
    { 
        public ValueMapping(object value, string displayMember) 
        { 
            _valueMember = value; 
            _displayMember = displayMember; 
        } 
 
        string _displayMember; 
 
        public string DisplayMember 
        { 
            get { return _displayMember; }             
        } 
 
        object _valueMember; 
 
        public object ValueMember 
        { 
            get { return _valueMember; }             
        } 
    } 
 

Tried with current version 2010 Q1 SP 1
Is there a workaround ?

Regards,
Markus

8 Answers, 1 is accepted

Sort by
0
Julian Benkov
Telerik team
answered on 04 May 2010, 06:15 PM
Hello Markus,

The current edition of RadGridView does not support this functionality out of the box, but you can use a custom filter as a workaround:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
 
        BindingList<Product2> list = new BindingList<Product2>();
        Product2 pr = new Product2();
        pr.ProductType = EProductType.Hard;
        pr.Name = "IBM1";
        list.Add(pr);
        pr = new Product2();
        pr.ProductType = EProductType.Soft;
        pr.Name = "VS2010";
        list.Add(pr);
 
        this.radGridView1.DataSource = list;
 
        this.radGridView1.Columns.RemoveAt(0);
        Telerik.WinControls.UI.GridViewComboBoxColumn comboColumn = new GridViewComboBoxColumn();
        this.radGridView1.Columns.Insert(0, comboColumn);
        comboColumn.CustomDataOperation = CustomDataOperation.Filtering;
        
        AddMapping();
        this.radGridView1.CustomFiltering += new GridViewCustomFilteringEventHandler(radGridView1_CustomFiltering);
 
         
    }
 
    void radGridView1_CustomFiltering(object sender, GridViewCustomFilteringEventArgs e)
    {
        if (e.Column is GridViewComboBoxColumn)
        {
            FilterExpression filterExpression = e.GridViewTemplate.FilterExpressions["ProductType"];
            if (filterExpression != null)
            {
                e.Visible = (((EProductType)e.CellValue).Equals(((ParameterValuePair)filterExpression.Parameters[0]).Value));
            }
            else
            {
                e.Visible = true;
            }
             
        }
    }
 
    private void AddMapping()
    {
        IList<ValueMapping> mappings = new List<ValueMapping>();
        mappings.Add(new ValueMapping(EProductType.None, ""));
        mappings.Add(new ValueMapping(EProductType.Hard, "Hardware"));
        mappings.Add(new ValueMapping(EProductType.Soft, "Software"));
 
        Telerik.WinControls.UI.GridViewComboBoxColumn comboColumn = radGridView1.Columns[0] as Telerik.WinControls.UI.GridViewComboBoxColumn;
        comboColumn.FieldName = "ProductType";
        comboColumn.UniqueName = "ProductType";
        comboColumn.DataSource = mappings;
        comboColumn.ValueMember = "ValueMember";
        comboColumn.DisplayMember = "DisplayMember";
        comboColumn.DataType = typeof(EProductType);
 
        this.radGridView1.EnableFiltering = true;
 
    }
}


Sincerely yours,
Julian Benkov
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
BENOIT CHEVALIER
Top achievements
Rank 1
answered on 15 Sep 2010, 04:15 PM
I'm working with the V 2009Q3 and I try to use your work around.

And I have the following problem:
- the first time I try to use the Filter : all is OK.
- the second time I get an IndexoutOfRangeException  from the grid :

Target Site : {Telerik.WinControls.UI.GridViewRowInfo GetRow(Int32)}

StackTrace : 
   à Telerik.WinControls.UI.DataGroup.GetRow(Int32 index)
   à Telerik.WinControls.UI.GridTraverser.MoveNextDataRow()
   à Telerik.WinControls.UI.GridTraverser.MoveNextRow()
   à Telerik.WinControls.UI.GridTraverser.MoveNextGroup()
   à Telerik.WinControls.UI.GridTraverser.MoveForwardCore()
   à Telerik.WinControls.UI.GridTraverser.MoveForward()
   à Telerik.WinControls.UI.GridTraverser.MoveNext()
   à Telerik.WinControls.UI.GridTableBodyElement.ArrangeOverride(SizeF finalSize)
   à Telerik.WinControls.RadElement.ArrangeCore(RectangleF finalRect)
   à Telerik.WinControls.RadElement.Arrange(RectangleF finalRect)
   à Telerik.WinControls.Layouts.ContextLayoutManager.UpdateLayout()
   à Telerik.WinControls.Layouts.ContextLayoutManager.UpdateLayoutCallback(ILayoutManager manager)


Can you please help me to have something working with the 2009Q3 telerik grid?
0
Markus
Top achievements
Rank 1
answered on 15 Sep 2010, 04:45 PM
Hello Benoit,

i cannot give you a solution for telerik's workaround. But I can show you how I solved it finally. I also use 2009Q3.

Add an extra property to your object which is not of an enum type. Instead it returns an integer.

public EProductType ProductType
{
    get { return _productType; }
    set { _productType = value; }
}
 
//new propery for binding grid column
public int GPProductType
{
    get { return (int)_productType; }
}

Now, bind grid column to the new property 'GPProductType'.

You also have to change the data type in ValueMapping to integer:

mappings.Add(newValueMapping((int)EProductType.None, ""));
mappings.Add(newValueMapping((int)EProductType.Hard, "Hardware"));
mappings.Add(newValueMapping((int)EProductType.Soft, "Software"));

This solutions works for me as an alternative to Telerik's workaround. See my first post for more details

Best regards,

Markus

0
Svett
Telerik team
answered on 20 Sep 2010, 05:37 PM
Hello guys,

@Markus: Thank you for your help in the forum!

@BENOIT CHEVALIER: You can use the work around that Markus posted. Nevertheless, I recommend upgrading to the latest version Q2 2010 SP2 (2010.2 10.914) where we have out of the box support for filtering.

All the best,
Svett
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
BENOIT CHEVALIER
Top achievements
Rank 1
answered on 14 Oct 2010, 11:32 AM
Hello
I'v some problems with the latest version of the grid (V 2010.2.10.914)

I have some objects with enum properties in the grid:

1st problem:
Step 1) I set the filter to "Equals To" and select one value of my enum "Value1".
Step 2) If I click on a cell containing the value "Value2" (which should disappear when the filter is applied) then all my form is frozen!!!

2d problem:
- If my datasource object contains too many properties, and I try to use filter (on enum) my form freeze because my cell is to small !

3th problem:
- Custom filter doesn't work on enums (Flag Enum, Nullable Enum )...

4 problem:
- Filtering on Nullable Type doesn't work anymore (but used to work with 2009 Q3 grid)!! 



0
Alexander
Telerik team
answered on 15 Oct 2010, 04:55 PM
Hello Benoit,

Thank you for your question.

These are known issues and we plan to address them in the scheduled for November Q3 release. The support of Flag Enums is an interesting request and we will consider implementing this functionality in a future version. 

To work around the first issue, you should replace the default RadGridView behavior:

this.radGridView1.GridBehavior = new CustomGridBehavior();
 
public class CustomGridBehavior : BaseGridBehavior
{
    protected override bool OnMouseDownLeft(MouseEventArgs e)
    {
        RadElement element = this.GetElementAtPoint(this.GridViewElement.ElementTree, e.Location);
        bool isFilterCell = element is GridFilterCellElement;
        if (!isFilterCell)
        {
            RadElement parentElement = element.Parent;
            while (parentElement != null)
            {
                if (parentElement is GridFilterCellElement)
                {
                    isFilterCell = true;
                    break;
                }
                parentElement = parentElement.Parent;
            }
        }
 
        if (!(isFilterCell) && this.GridViewElement.CurrentCell is GridFilterCellElement)
        {
            this.GridViewElement.EditorManager.EndEdit();
        }
 
        return base.OnMouseDownLeft(e);
    }
 
    private RadElement GetElementAtPoint(ComponentElementTree componentTree, Point point)
    {
        RadElement elementUnderMouse = componentTree.GetElementAtPoint(point);
 
        while (elementUnderMouse != null)
        {
            RadElement element = elementUnderMouse as RadElement;
 
            if (element != null)
            {
                return element;
            }
 
            elementUnderMouse = elementUnderMouse.Parent;
        }
 
        return null;
    }
}

As to the second issue, you can use the GridViewColumn MinWidth property to prevent the column with enumeration from getting too narrow.

I hope it helps. Do not hesitate to write back, if you have more questions.

Best regards,
Alexander
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Martin
Top achievements
Rank 1
answered on 04 Apr 2011, 05:54 PM
Hi,

Nice workaround but unfortunately does not work now in Q1 2011 !

There is no Column property on the event args now.

Any other advice on how to do this would be gratefully received before we have to move to ComponentOne grids.

Regards
Martin
0
Alexander
Telerik team
answered on 07 Apr 2011, 09:08 AM
Hello Martin,

Thank you for contacting us.

I confirm that in the Q1 2011 released there is an introduced issue regarding Enum types filtering in RadGridView. We will address it in our next release. As a workaround, you can create a custom GridFilterCellElement and use it instead of the default cell:
public class CustomFilterCellElement : GridFilterCellElement
{
    public CustomFilterCellElement(GridViewDataColumn column, GridRowElement row)
        : base(column, row)
    {
    }
 
    protected override string ApplyFormatString(object value)
    {
        if (value == null)
        {
            return null;
        }
        return value.ToString();
    }
 
    protected override Type ThemeEffectiveType
    {
        get
        {
            return typeof(GridFilterCellElement);
        }
    }
}
 
private void radGridView1_CreateCell(object sender, GridViewCreateCellEventArgs e)
{
    if (e.CellType == typeof(GridFilterCellElement))
    {
        e.CellType = typeof(CustomFilterCellElement);
    }
}

I hope it helps.

Best regards,
Alexander
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
GridView
Asked by
Markus
Top achievements
Rank 1
Answers by
Julian Benkov
Telerik team
BENOIT CHEVALIER
Top achievements
Rank 1
Markus
Top achievements
Rank 1
Svett
Telerik team
Alexander
Telerik team
Martin
Top achievements
Rank 1
Share this question
or