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
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
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
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
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: