Declined
Last Updated: 19 Nov 2018 16:15 by ADMIN
ADMIN
Dimitar
Created on: 13 Nov 2018 13:37
Category: GridView
Type: Bug Report
0
FIX. RadGridView - setting the DataType of the column to decimal does not affect the sorting
To reproduce:
- Use a column with numbers stored as strings.
- Set the data type to decimal
- The columns should be sorted according to the number value not alphabetically.

Workaround:
var col = new GridViewTextBoxColumn();
col.DataTypeConverter = new DecimalConverter();
col.FieldName = "Dosage";
col.UseDataTypeConverterWhenSorting = true;
col.DataType = typeof(decimal);
radGridView1.Columns.Add(col);

public class DecimalConverter : TypeConverter
{
    public override bool CanConvertTo(ITypeDescriptorContext context, Type destination_type)
    {
        
        if (destination_type == typeof(decimal))
        {
            return true;
        }

        return base.CanConvertTo(context, destination_type);
    }
    public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destination_type)
    {
        if (destination_type == typeof(decimal))
        {
            return decimal.Parse(value.ToString());
        }

        return base.ConvertTo(context, culture, value, destination_type);
    }
}
1 comment
ADMIN
Ivan Petrov
Posted on: 19 Nov 2018 15:15
The reason for this behaviour is that decimal is not a primitive type as are int, double or float. This causes the DecimalConverter's CanConvertTo method to return false when one passes decimal as the target type for the conversion. If you want to keep using decimal as the column's data type you should use the converter provided in the workaround above. If your case allows you to, you should use a primitive type that will work right away. Note that the UseDataTypeConverterWhenSorting property should be set to true.