This is a migrated thread and some comments may be shown as answers.

Format string problem

3 Answers 119 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Jota
Top achievements
Rank 1
Jota asked on 05 Oct 2011, 07:31 PM
Hi...

I have a radgridview linked to a datasource.

One of the columns (a decimal column) takes the data from an integer value in the database... this value represents a time, but I can´t change the type in the database.

I want to show the value in a HH:MM:SS format.
To do that in other situations I just set a string.Format this way:
string.format("{0:00}:{1:00}:{2:00}", value/3600, (value%3600)/60,(value%3600)%60)

Is there a way to apply this custom format to the column?

I´ve tried using a date format in the FormatString property of the columm this way: {0:hh.mm.ss} but then the columm shows data like this "hh.Valuemm.ss" (Value is the integer in the column, like 4 or whatever).

Any help will be welcomed

Thanks in advance

3 Answers, 1 is accepted

Sort by
0
Accepted
Emanuel Varga
Top achievements
Rank 1
answered on 06 Oct 2011, 10:10 AM
Hello,

I have a proposal for you, it's not a very clean one but it will do what you need,
You should handle the CellFormatting event and just format the displayed text in the cell
Something like this:
void radGridView1_CellFormatting(object sender, CellFormattingEventArgs e)
{
    if (e.Column.FieldName != "Time")
    {
        return;
    }
 
    e.CellElement.Text = GetText(e.CellElement.Value);
}
 
private string GetText(object value)
{
    if (value == null || string.IsNullOrEmpty(value.ToString()) || !(value is int))
    {
        return null;
    }
    var intValue = Convert.ToInt32(value);
    var seconds = intValue % 100;
    var minutes = (intValue / 100) % 100;
    var hours = (intValue / 10000) % 100;
    return string.Format("{0,2:00}:{1,2:00}:{2,2:00}", hours, minutes, seconds);
}

Hope this helps, if you have any other questions or comments, please let me know,

Best Regards,
Emanuel Varga

Telerik WinForms MVP
0
Jota
Top achievements
Rank 1
answered on 06 Oct 2011, 03:59 PM
Thanks a lot Emanuel... that made the trick...

0
Ivan Petrov
Telerik team
answered on 07 Oct 2011, 05:04 PM
Hello Jota,

Thank you for writing.

The solution Emanuel has provided is the best approach for this scenario.

If you have other questions, do not hesitate to write back. 

Best wishes,
Ivan Petrov
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

Tags
GridView
Asked by
Jota
Top achievements
Rank 1
Answers by
Emanuel Varga
Top achievements
Rank 1
Jota
Top achievements
Rank 1
Ivan Petrov
Telerik team
Share this question
or