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

DataTypeConverter not working with filtering

1 Answer 32 Views
GridView
This is a migrated thread and some comments may be shown as answers.
John
Top achievements
Rank 1
John asked on 19 Jun 2014, 06:52 PM
I am having a problem using filtering with a column which has a DataTypeConverter specified.

My object has a bool value which I have bound to a text box column.  I use a TypeConverter to convert this bool to a string (and back); true converts to "group", false converts to "individual".  This works fine except when I try to use filtering.  I chose "equals" and entered "individual"; I expected my type converter to be called so it could convert "invidual" to false, but the type converter was not getting called at all.  When I entered "false" into the filter my type converter was finally called but was passed a bool; it wanted me to convert bool to bool?!.  It seems that the filter is converting the string to bool itself and not using my type converter.  After I typed in "false" the filter showed "Equals: individual" and seemed to work correctly. 

This  behavior is a problem since I cannot ask my end users to type in "true" and "false" and remember which maps to which display value.  It seems like not using my specified TypeConverter to convert the string back to the match value is a bug. 

Can you suggest a work around?  Will this be fixed?

1 Answer, 1 is accepted

Sort by
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 24 Jun 2014, 11:07 AM
Hello John,

Thank you for writing.

Here is a sample code snippet with custom TypeConverter which is used to convert bool property to string and vice versa. As a result, the user is allowed to perform filtering and type in "group"/"individual" instead of true/false in the filter cell:
public Form1()
{
    InitializeComponent();
 
    BindingList<Item> items = new BindingList<Item>();
    for (int i = 0; i < 20; i++)
    {
        items.Add(new Item(i,i + ".Title",i % 2 == 0));
    }
 
    this.radGridView1.AutoGenerateColumns = false;
 
    GridViewTextBoxColumn textBoxColumnId = new GridViewTextBoxColumn();
    textBoxColumnId.FieldName = "Id";
    radGridView1.Columns.Add(textBoxColumnId);
 
    GridViewTextBoxColumn textBoxColumnTitle = new GridViewTextBoxColumn();
    textBoxColumnTitle.FieldName = "Title";
    radGridView1.Columns.Add(textBoxColumnTitle);
 
    GridViewTextBoxColumn textBoxColumnIsActive = new GridViewTextBoxColumn();
    textBoxColumnIsActive.FieldName = "IsActive";
    textBoxColumnIsActive.DataTypeConverter = new MyConverter();
    radGridView1.Columns.Add(textBoxColumnIsActive);
 
    radGridView1.DataSource = items;
    radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
    radGridView1.EnableFiltering = true;
}
 
public class MyConverter:TypeConverter
{
    public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
    {
        return destinationType == typeof(string);
    }
 
    public override object ConvertTo(ITypeDescriptorContext context,
        CultureInfo culture, object value, Type destinationType)
    {
        if (destinationType == typeof(string))
        {
            string convertedTo = (bool)value ? "group" : "individual";           
            return convertedTo;
        }
        return base.ConvertTo(context, culture, value, destinationType);
    }
 
    public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
    {
        return sourceType == typeof(string);
    }
 
    public override object ConvertFrom(ITypeDescriptorContext context,
        CultureInfo culture, object value)
    {
        if (value is string)
        {
            bool convertedFrom = value.ToString() == "group" ? true : false;
            return convertedFrom;
        }
        return base.ConvertFrom(context, culture, value);
    }
}
 
public class Item
{
    public int Id { get; set; }
 
    public string Title { get; set; }
 
    public bool IsActive { get; set; }
 
    public Item(int id, string title, bool isActive)
    {
        this.Id = id;
        this.Title = title;
        this.IsActive = isActive;
    }
}

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

Regards,
Desislava
Telerik
 
Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
 
Tags
GridView
Asked by
John
Top achievements
Rank 1
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Share this question
or