Completed
Last Updated: 15 Apr 2016 12:07 by ADMIN
ADMIN
Dess | Tech Support Engineer, Sr.
Created on: 14 Mar 2016 12:11
Category: GridView
Type: Bug Report
0
FIX. RadGridView - Excel-like filtering doesn't work when using a TypeConverter
To reproduce:

public Form1()
{
    InitializeComponent();
    BindingList<Item> items = new BindingList<Item>();
    
    for (int i = 0; i < 30; i++)
    {
        if (i % 3 == 0)
        {
            items.Add(new Item(i,"Item" + i, IndeterminateBoolean.YesAndNo));
        }
        else if (i % 3 == 1)
        {
            items.Add(new Item(i, "Item" + i, IndeterminateBoolean.Yes));
        }
        else
        {
            items.Add(new Item(i, "Item" + i, IndeterminateBoolean.No));
        }
    }
    this.radGridView1.DataSource = items;
    GridViewCheckBoxColumn checkBox = new GridViewCheckBoxColumn("CheckBoxCol");
    checkBox.DataTypeConverter = new IndeterminateBooleanToggleStateConverter();
    checkBox.FieldName = "IsActive";
    checkBox.ThreeState = true;
    this.radGridView1.Columns.Add(checkBox);
    this.radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
    this.radGridView1.EnableFiltering = true;

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

public class Item
{
    public int Id { get; set; }

    public string Name { get; set; }

    public IndeterminateBoolean IsActive { get; set; }

    public Item(int id, string name, IndeterminateBoolean isActive)
    {
        this.Id = id;
        this.Name = name;
        this.IsActive = isActive;
    }
}

public enum IndeterminateBoolean
{
    No,
    Yes,
    YesAndNo
}

public class IndeterminateBooleanToggleStateConverter : TypeConverter
{
    public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
    {
        return sourceType == typeof(ToggleState);
    }
    
    public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
    {
        return destinationType == typeof(ToggleState);
    }

    public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
    {
        if (value is ToggleState)
        {
            switch ((ToggleState)value)
            {
                case ToggleState.On:
                    return IndeterminateBoolean.Yes;
                case ToggleState.Off:
                    return IndeterminateBoolean.No;
                case ToggleState.Indeterminate:
                default:
                    return IndeterminateBoolean.YesAndNo;
            }
        }
        else
        {
            return base.ConvertFrom(context, culture, value);
        }
    }
    
    public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
    {
        if (destinationType == typeof(ToggleState))
        {
            if (value is IndeterminateBoolean)
            {
                switch ((IndeterminateBoolean)value)
                {
                    case IndeterminateBoolean.Yes:
                        return ToggleState.On;
                    case IndeterminateBoolean.No:
                        return ToggleState.Off;
                    case IndeterminateBoolean.YesAndNo:
                    default:
                        return ToggleState.Indeterminate;
                }
            }
            if (value is string)
            {
                switch (((string)value ?? string.Empty).Trim())
                {
                    case "Yes":
                        return ToggleState.On;
                    case "No":
                    case "":
                        return ToggleState.Off;
                    case "Both":
                    default:
                        return ToggleState.Indeterminate;
                }
            }
            else
            {
                return base.ConvertTo(context, culture, value, destinationType);
            }
        }
        else
        {
            return base.ConvertTo(context, culture, value, destinationType);
        }
    }
}

Workaround: use the custom filtering functionality: http://docs.telerik.com/devtools/winforms/gridview/filtering/custom-filtering

You can access the RadGridView.FilterDescriptors collection and according to the FilterDescriptor.Expression property to determine whether the row will be visible or not. 
(Total attached files size should be smaller than 20mb. Allowed extensions: .zip, .rar, .jpg, .png, .gif)
0 comments