开发者

Sort correctly by time open

开发者 https://www.devze.com 2023-03-23 09:42 出处:网络
I have a column in a XtraGrid that shows the time elapsed since the record was inserted. I ca开发者_开发技巧lculate this on the server and returning the time as a string.

I have a column in a XtraGrid that shows the time elapsed since the record was inserted. I ca开发者_开发技巧lculate this on the server and returning the time as a string.

The problem is that its not sorted correctly, Please see image how 9 days is more then 23 days...

Sort correctly by time open

Any idea how to make it in a way the sort would work better?


One way to get around this is to store your values in the Database as a int64 as Ticks, therefore you can do sorting on the server side, then on your application side, create TimeSpans from the Ticks and you can handle the CustomColumnDisplayText to display it however you want, ensuring your column is set to sort on Value and not DisplayText.

Example

Within your CustomDisplayText event of your GridView place

TimeSpan span = TimeSpan.FromTicks(e.Value);
e.DisplayText = string.Format("{0} days {1} hours {2} minutes", span.Days, span.Hours, span.Minutes);


You could sort it on the server as part of your SQL statement (I'm assuming youre getting this out of a DB). Your other option is to write your own comparison function which would involve parsing the time back into some kind of timespan and sorting on that.

0

精彩评论

暂无评论...
验证码 换一张
取 消