Domanda

I want to convert a number to a time, like

10  ->  00:10  
55  ->  00:55  
75  ->  01:15  
90  ->  01:30  
705 ->  11:45 

and so on. I know I can write a method and do this convertion, but I need it for a DataGridView so I'd better use a string Format.

As far as I know there is not such a thing implemented. How I can implement this format so that I can set it on the Grid's DefaultCellStyle.Format?

È stato utile?

Soluzione

You can create a TimeSpan from the minutes first:

TimeSpan minutes = TimeSpan.FromMinutes(705);

Then use DateTime.Add:

DateTime dt = DateTime.Today.Add( minutes );

Now you can use DateTime.ToString:

string text = dt.ToString("HH:mm");

You could also use TimeSpan.ToString(although DateTime.ToString is more readable):

text = minutes.ToString(@"hh\:mm")

You can use the DataGridViews CellFormatting event.

How to: Customize Data Formatting in the Windows Forms DataGridView Control

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top