Completed
Last Updated: 22 Jul 2015 15:29 by ADMIN
ADMIN
Ralitsa
Created on: 09 Jul 2015 08:21
Category: GridView
Type: Bug Report
1
FIX. RadGridView - Sorting a column containing nulls causes an exception.
To reproduce: 
1. Add a GridViewDecimalColumn to a grid with data type int, float, decimal, double, real, money. 
2. Add few rows where one of rows contains null value. 
3. Run the form. Sort the grid by any of column with null data and you will see that the exception is thrown. 

Workaround: 
1. Use custom TypeConverter

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

    public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
    {
        if (destinationType == typeof(float) && (value == null || value is DBNull))
        {
            return float.MinValue;
        }
        return value;
    }
}

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

    public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
    {
        if (destinationType == typeof(int) && (value == null || value is DBNull))
        {
            return int.MinValue;
        }
        return value;
    }
}

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

    public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
    {
        if (destinationType == typeof(decimal) && (value == null || value is DBNull))
        {
            return decimal.MinValue;
        }
        return value;
    }
}

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

    public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
    {
        if (destinationType == typeof(double) && (value == null || value is DBNull))
        {
            return double.MinValue;
        }
        return value;
    }
}

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

    public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
    {
        if (destinationType == typeof(Single) && (value == null || value is DBNull))
        {
            return Single.MinValue;
        }
        return value;
    }
}

2. Apply the converter to GridViewDecimalColumn

this.radGridView2.Columns["ColumnReal"].DataTypeConverter = new CustomSingleConverter();
this.radGridView2.Columns["ColumnFloat"].DataTypeConverter = new CustomDoubleConverter();
this.radGridView2.Columns["ColumnDecimal"].DataTypeConverter = new CustomDecimalConverter();
this.radGridView2.Columns["ColumnInt"].DataTypeConverter = new CustomIntConverter();
this.radGridView2.Columns["ColumnNumeric"].DataTypeConverter = new CustomDecimalConverter();
this.radGridView2.Columns["ColumnMoney"].DataTypeConverter = new CustomDecimalConverter();
2 comments
ADMIN
Ivan Petrov
Posted on: 17 Jul 2015 12:23
The issue here is caused by the fact that we now use the column's DataTypeConverter when sorting. As default TypeConverters of the primitive numeric types cannot convert null and DBNull values developers should handle these cases with a custom type converter. To avoid forcing all developers to create such converters, we will introduce a new property called UseDataTypeConverterWhenSorting for data columns.
erwin
Posted on: 10 Jul 2015 07:41
Run into this bug too. showstopper for 2015Q2. 
The suggested workaround with adding CustomConverters to every grid is not really an option for large applications.