Telerik blogs

One of the new features that will come with the next version of RadGridView is the new data conversion layer.

In our daily work we have often got into troubles in visualizing and formatting concrete types as other types. Sometimes the data source is not compatible with the bind target. Hence, you need to create a facade that converts the data to desired type.

Let’s say that you bind the grid to a source that is incompatible with the columns that you want to show, for example a GridViewCheckBoxColumn for char field instead of GridViewTextBoxColumn. How can you accomplish that?

screenshot

The new version of RadGridView will give you a flexible way to adopt incompatible types by using TypeConverter subclasses that take care of this. Let me show you the type conversion highlights.

The TypeConverter subclasses are used to convert values between data types. You can read more about that in MSDN. In our scenario we need custom TypeConverter that converts from char type to ToggleState type and vice versa. Let’s say that we implemented the ToggelStateConverter class that converts char values Y and N to ToggleState values ToggleState.On and ToggleState.Off.

There are two ways to plug it in RadGridView’s conversion layer:

The first approach will be to create a desired column and assign its DataTypeConverter property:

GridViewCheckBoxColumn checkBox = new GridViewCheckBoxColumn("Organic", "IsOrganic");
checkBox.DataTypeConverter = new ToggleStateConverter();
this.gridView.Columns.Add(checkBox);

The second way is by using the TypeConverterAttribute. It allows you to specify TypeConverter for any property in your business object. When you set it as data source of RadGridView, you cause creation of GridViewCheckBoxColumn instead of GridViewTextBoxColumn.

The following code snippet is a typical example for that:

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

    public string ProductName { get; set; }
    public string Category { get; set; }
    public int UnitsInStock { get; set; }

    [TypeConverter(typeof(UnitPriceConverter))]
    public double UnitPrice { get; set; }

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

    [TypeConverter(typeof(DateTimeConverter))]
    public double DeliveryDate { get; set; }
}

 

Last but not at leas,t RadGridView’s conversation layer will allow handling and manipulating null values. You can set the desired value that has to be shown if data source’s field value is null. You can use GridViewDataColumn’s property NullValue to set it:

this.gridView.Columns["Category"].NullValue = "Type product's category";

 

Also you can specify the default value that is committed to the source if the cell’s value is changed to null. GridViewDataColumn’s property DataSourceNullValue determines that:

this.gridView.Columns["Category"].DataSourceNullValue = "Beverages";

 

We welcome any suggestions or comments about these features!


About the Author

Nikolay Diyanov

Diyanov is the Product Manager of the Native Mobile UI division at Progress. Delivering outstanding solutions that make developers' lives easier is his passion and the biggest reward in his work. In his spare time, Nikolay enjoys travelling around the world, hiking, sun-bathing and kite-surfing.

Find him on Twitter @n_diyanov or on LinkedIn.

Related Posts

Comments

Comments are disabled in preview mode.