Format a TimeSpan in a datagridview?

cjard

Well-known member
Joined
Apr 25, 2006
Messages
7,081
Programming Experience
10+
I have an issue currently where the only formatting option I can find for my TimeSpan to prevent it showing the fractions of the second, is to remove them from the TimeSpan. I'd really like to set a format of hh:mm:ss for my timespan, and keep the fractional seconds as they are used in album duration calculation and DO make a difference for the purposes to which i'm applying them

For example, my grid looks like:

00:05:30.0400000
00:03:59.9860000
00:04:15
00:05:00
00:05:05.5870000
00:04:14.7470000
00:03:15.2260000

the two shorter ones are by pure coincidence exactly on the second - the TimeSpan have no milliseconds component. Unlike Date, TimeSpan doesnt have a formattabke ToString, does anyone have any other ideas for showing them as hh:mm:ss but retaining their millisecond component in the data backend?
 

I know.. nuisance isnt it?

1 Override of Object.ToString():
hh:mm:ss[:ffffff]


Meaning, it isnt formattable, DataGridView will call .ToString on it and if the timespan contains a fractional component then it will be displayed whether i like it or not.

Options:
Convert them to dates, make all my TimeSPans some number of minutes and seconds past midnight, on the day i was born (and why not, indeed?) then just format them to show the minutes/seconds

Create a custom column/cell renderer on the DGV that extends DataGridViewTextBox and knows jsut what to do with timespans

Remove the fractional components from the timespans, to another column..


Ahh, so much hard work!
 
Back
Top