开发者

How to show time intervals in datagridview?

I am working on an application in which I have two database fields. One is the Date, and the other is the time of the insertion of the record with d开发者_JS百科ata type VARCHAR(10).

I need to fill the DataGridView with these fields.

The problem is that I have to fill the time as the elapsed time between those datetimes.

If the record is added at 2010/08/20 18:00:00.

  • if showing the data after 2 minutes then it will show 2 Mins Ago
  • if after 2 hrs then 2 Hrs Ago
  • if 2 days then 2 Days Ago.


At first you have to calculate the TimeSpan simply by substracting it from DateTime.Now. Take this TimeSpan and also this answer (or any other within this question), to write a function that takes this TimeSpan and returns the needed string.


Do the select for that column as a timespan: now - then. Then convert to any time unit you need.


Consider implementing an extension method on System.DateTime. Perhaps call your extension method something like ToRelativeTime(). It sounds like your second date is a varchar, so you'll have to do some work to convert that to datetime.

Here's an example DateTime method that will subtract a given DateTime from Now. The modification you'd look for is something like:

private static string ToRelativeDateTime(DateTime someDate, 
                                         DateTime rowInsertedAt)

In your DataGridView, you can then set that column's value to be the return or output of the extension method.

You don't necessarily HAVE to make an extension method on DateTime, but perhaps a static member method on its own.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜