New to Telerik UI for WinForms? Download free 30-day trial

Converting Data Types

Overview

The GridViewDataColumn provides a unified way of converting incompatible value types to a type supported by the column instance. The standard .NET Framework TypeConverter subclasses are used to convert values between data types.

For instance, instead of using GridViewTextBoxColumn to display char fields you want to use a GridViewCheckBoxColumn, implement a custom Type Converter class which to determine how RadGridView recognizes this type. For more information, see. How to: Implement a Type Converter in MSDN.

As a quick example, let’s say that we want to convert char values Y and N to ToggleState values ToggleState.On and ToggleState.Off. To do this we will need to implement the ToggelStateConverter class that converts these values:

ToggleState converter


public class ToggleStateConverter : TypeConverter
{
    public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
    {
        return destinationType == typeof(ToggleState) || destinationType == typeof(bool);
    }
    public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
    {
        if (value is char && destinationType == typeof(ToggleState))
        {
            char charValue = (char)value;
            switch (charValue)
            {
                case 'Y':
                    return ToggleState.On;
                case 'N':
                    return ToggleState.Off;
                default:
                    return ToggleState.Indeterminate; 
            }
        }
        else if (value is bool && destinationType == typeof(char))
        {
            bool boolValue = (bool)value;
            switch (boolValue)
            {
                case true:
                    return 'Y';
                case false:
                    return 'N';
                default:
                    return 'M';
            }
        }
        return base.ConvertTo(context, culture, value, destinationType);
    }
    public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
    {
        return sourceType == typeof(ToggleState) || sourceType == typeof(bool);
    }
    public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
    {
        ToggleState state;
        bool boolValue;
        if (value is ToggleState)
        {
            state = (ToggleState)value;
            switch (state)
            {
                case ToggleState.On:
                    return 'Y';
                case ToggleState.Off:
                    return 'N';
                default:
                    return 'M';
            }
        }
        else if (value is bool)
        {
            boolValue = (bool)value;
            switch (boolValue)
            {
                case true:
                    return 'Y';
                case false:
                    return 'N';
                default:
                    return 'M';
            }
        }
        return base.ConvertFrom(context, culture, value);
    }
}

Applying Type Converters

There are two ways to apply converters to the RadGridView conversion layer:

Setting the DataTypeConverter property of the column

The first approach to apply type converters is to create the desired column and assign its DataTypeConverter property. This approach is handy when you using non-business objects (such as DataTable) or your business objects’ properties do not have TypeConverterAttribute applied.

Applying TypeConverter


private void ConvertingDataTypes_Load(object sender, EventArgs e)
{
    GridViewCheckBoxColumn checkBox = new GridViewCheckBoxColumn("Organic", "IsOrganic");
    checkBox.DataTypeConverter = new ToggleStateConverter();
    this.radGridView1.Columns.Add(checkBox);
}

Applying System.ComponentModel.TypeConverterAttribute to the incompatible property of the business object used as a data source

The second way to add type converters is to use the TypeConverterAttribute, which allows you to specify the TypeConverter for any property in your business object. When you set it as a data source for RadGridView, you create GridViewCheckBoxColumn instead of GridViewTextBoxColumn. This approach is handy when you are creating your own business objects with TypeConverter attribute applied.

Custom class with TypeConverter attribute


public class Product
{
    public int ProductID { get; set; }

    public string ProductName { get; set; }

    public string Category { get; set; }

    public int UnitsInStock { get; set; }
    public double UnitPrice { get; set; }

    [TypeConverter(typeof(ToggleStateConverter))]
    public char IsOrganic { get; set; }
    public double DeliveryDate { get; set; }
}

Handling Null Values

The RadGridView’s conversation layer can handle null values. You can specify the default value that is committed to the source if the cell value is changed to null. GridViewDataColumn’s DataSourceNullValue property can be set using the following code snippet:

Handling null values


this.radGridView1.Columns["ProductName"].DataSourceNullValue = "ENTER PRODUCT NAME";

Using the TypeConverter when sorting.

The type converter can be used when the column is sorted as well. To enable this functionality you should set the UseDataTypeConverterWhenSorting property of the column.

You should consider that the column can contain null values and handle this in the custom type converter class as well. The following code snipped shows a custom float type converter that handles null values:

Handling null values when sorting

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;
    }
}