Hi!
I'm populating a grid with data from a oracle database, like this :
"select name, to_char(date,'DD-MON-YYYY HH24:MI:SS') from atable".
So when using the sort functionality in the grid, the values are sorted by their string value not their actual date value. But i don't like this, and i would like to still have the date formated but when sorted to be sorted as a datetime column. How do you recomment i should do this ?
Thank you!
The best thing to do is.. don't use strings for dates. Doing so will cause problems in a number of areas like sorting, filtering, and calculations.You would be better off getting the dates as DateTime structures and then applying formatting in the UI. The grid column has a Format property which you could use to display the dates in the format you want without converting them to strings.
But, if for some reason, you cannot do that, then the alternative would be to write a SortComparer for the column that converts the strings into dates and compares them.
Thanks Mike and Hristo!
I've tried the SortComparer solution, and wrote this comparer :
public class DateTimeComparer : IComparer { internal DateTimeComparer() { } int IComparer.Compare(object x, object y) { try { UltraGridCell xCell = (UltraGridCell)x; UltraGridCell yCell = (UltraGridCell)y; DateTime dtx = DateTime.Parse(xCell.Value.ToString()); DateTime dty = DateTime.Parse(yCell.Value.ToString()); return DateTime.Compare(dtx, dty); } catch (Exception ex) { return 0; } } }
public class DateTimeComparer : IComparer {
internal DateTimeComparer() {
}
int IComparer.Compare(object x, object y) {
try {
UltraGridCell xCell = (UltraGridCell)x;
UltraGridCell yCell = (UltraGridCell)y;
DateTime dtx = DateTime.Parse(xCell.Value.ToString());
DateTime dty = DateTime.Parse(yCell.Value.ToString());
return DateTime.Compare(dtx, dty);
catch (Exception ex) {
return 0;
But unfortunately, it doesn't seem to apply. I still get an ordering based on the string values.
Am i doing something wrong in the code?