Sorting dates in grids using SmartDate

Sorting dates in grids using SmartDate

Old forum URL: forums.lhotka.net/forums/t/4840.aspx


Marjon1 posted on Tuesday, May 13, 2008

When using a smart date I know the recommendation is to expose it as a string property and have the field as a smart date. If I use something like _LastModifiedDateTime.ToString("g") which outputs the information like D-MM-YYYY HH:MM (using Australian local) this exactly how I want it formatted, but the sorting is all wrong when using any any grid control (primarly DevExpress XtraGrid).

Therefore what I've done is set the default text format for SmartDates to be "s", and am exposing a SmartDate property. In the CustomColumnDisplayText event I then change the displaytext to be SmartDate.ToString("g") or whatever format is correct. This then sorts the dates perfectly, can anyone think of any reason why this would be a bad thing?

This 'pattern' if you want to really abuse the word, would only be used on readonly properties for lists. Therefore I don' t have to worry about users entering anything invalid or null dates.

Marjon

ajj3085 replied on Wednesday, May 14, 2008

Ok, I may be way off base here, but I'll throw out the idea and you may find I'm right, or you'll find nothing because I'm wrong.. but..

I think SortedBindingList will handle this for you.  Are you wrapping your ROLB in a SBL before handing it to the binding source / grid?


Copyright (c) Marimer LLC