Wednesday, 19 December 2007

Telerik RadGrid V5.0 Sorting Bug with Nullable DateTime Types - "Property [ColumnName] does not support IComparable"

It turns out the Telerik RadGrid does not support sorting for nullable types (e.g. DateTime?) out of the box. For example, if you use a LINQ datasource for your grid based on a column that is nullable in SQL Server, you will get an error when trying to sort the column "Property [BoundColumnName] does not implement IComparable".

The simplest fix for this is to just make your column non-nullable if this is possible in your business situation.

3 comments:

Vladimir said...

David,
Sorting on nullable int with RadGrid can be found here:
http://www.telerik.com/demos/aspnet/Grid/Examples/Programming/Binding/DefaultCS.aspx

As to the LinqDataSource as far as I know this control will sort the data by default - most probably LinqDataSource is causing this problem.

Vlad

David Klein said...

The problem occurs without LINQ as well. e.g. if you are binding to a string that happens to have null values null, then you cannot implement IComparable as null is not comparable. You can work around this in your object/DTO by returning a comparable value (e.g. empty string) in place of a null.
e.g.

[EntityPropertyMapping("Fund")]
public string Fund
{
get { return _fund ?? string.Empty; }
set { _fund = value; }
}

[EntityPropertyMapping("DocumentType")]
public string DocumentType
{
get { return _documentType ?? string.Empty; }
set { _documentType = value; }
}

Anonymous said...

no such thing as "nullable int". Value types cannot be null in .net