Completed
Last Updated: 24 Aug 2015 13:05 by ADMIN
ADMIN
Dess | Tech Support Engineer, Sr.
Created on: 11 Nov 2014 09:04
Category: GridView
Type: Bug Report
0
FIX. RadGridView - FormatException when filtering and a GridViewCheckBoxColumn is available with custom TypeConverter
Note: InvalidCastException in case of converting Char to Boolean.

To reproduce: use the following code snippet:

public Form1()
{
    InitializeComponent();

    DataTable dt = new DataTable();
    dt.Columns.Add("Id", typeof(string));
    dt.Columns.Add("Name", typeof(string));
    dt.Columns.Add("Active", typeof(string));

    dt.Rows.Add("1", "Item1", "Y");
    dt.Rows.Add("2", "Item2", "N");

    this.radGridView1.DataSource = dt;
    this.radGridView1.Columns.RemoveAt(2);
    GridViewCheckBoxColumn checkBoxColumn = new GridViewCheckBoxColumn();
    checkBoxColumn.FieldName = "Active";
    checkBoxColumn.DataTypeConverter = new ToggleStateConverter();
    radGridView1.MasterTemplate.Columns.Add(checkBoxColumn);
    radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
    radGridView1.EnableFiltering = true;
}

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

    public override object ConvertTo(ITypeDescriptorContext context, 
        CultureInfo culture, object value, Type destinationType)
    {
        string charValue = value + "";

        switch (charValue)
        {
            case "Y":
                return ToggleState.On;
            case "N":
                return ToggleState.Off;
            case "M":
                return ToggleState.Indeterminate;
        }

        return base.ConvertTo(context, culture, value, destinationType);
    }

    public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
    {
        return sourceType == typeof(ToggleState);
    }

    public override object ConvertFrom(ITypeDescriptorContext context, 
        CultureInfo culture, object value)
    {
        ToggleState state = (ToggleState)value;

        switch (state)
        {
            case ToggleState.On:
                return "Y";
            case ToggleState.Off:
                return "N";
            case ToggleState.Indeterminate:
                return "M";
        }

        return base.ConvertFrom(context, culture, value);
    }
}

Try to filter by "Name" column. As a result FormatException  occurs because of inability to convert "Y"/"N" to Boolean although a custom TypeConverter is implemented to handle it.
0 comments