Format string problem

4 posts, 1 answers
  1. Jota
    Jota avatar
    2 posts
    Member since:
    Apr 2008

    Posted 05 Oct 2011 Link to this post

    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
  2. Answer
    Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 06 Oct 2011 Link to this post

    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
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Jota
    Jota avatar
    2 posts
    Member since:
    Apr 2008

    Posted 06 Oct 2011 Link to this post

    Thanks a lot Emanuel... that made the trick...

  5. Ivan Petrov
    Admin
    Ivan Petrov avatar
    701 posts

    Posted 07 Oct 2011 Link to this post

    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 >>

Back to Top