Domanda

I want to convert a number indicating seconds to my specific format mmmm:ss for example the number 3601 should result to 60:01 and 7201 results to 1200:01

For this purpose I have written the below code

var text = string.Format("{0}:{1}",seconds / 60, seconds % 60);
//seconds 651 outputs 10:51
//seconds 6612 outputs 110:12

which works fine, I just need to make a format out of this, something like

var text = string.Format("{x:x/60:x%60},seconds);

Is it possible to make custom formats? I insist on being done as a formatted string because I want to set it on a DataGridView.DefaultCellStyle.Format property, where I could not inject code.

È stato utile?

Soluzione

You can use the DataGridView.CellFormatting event for formatting the time value.

void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
    if (e.ColumnIndex == ColumnIndex)
    {
        e.Value = // formatted value
        e.FormattingApplied = true;
    }
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top