Format string for time span?

6 posts, 0 answers
  1. Jill-Connie Lorentsen
    Jill-Connie Lorentsen avatar
    118 posts
    Member since:
    Nov 2008

    Posted 21 Aug 2009 Link to this post

    In my gridview I have a column which shows the duration of the listed music tunes.

    From the database I get this duration in seconds, but I would like to display it as hh:mm:ss

    What will the format string be in this case?



    Regards, Jill-Connie Lorentsen

  2. erwin
    erwin avatar
    358 posts
    Member since:
    Dec 2006

    Posted 23 Aug 2009 Link to this post

    I used the CellFormatting Event to custom format
    timespan values.

    Maybe there is a more elegant way?

    Regards
    Erwin
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Nick
    Admin
    Nick avatar
    767 posts

    Posted 24 Aug 2009 Link to this post

    Hello Jill-Connie Lorentsen,

    CellFormatting is a good place to do the custom formatting. Thank you Erwin I have updated your Telerik points.

    private void Form5_Load(object sender, EventArgs e) 
            { 
                DataTable t = new DataTable(); 
                t.Columns.Add("A"typeof(int)); 
                t.Rows.Add(61); 
                t.Rows.Add(8); 
                t.Rows.Add(1243); 
                this.radGridView1.DataSource = t; 
                this.radGridView1.Columns[0].ReadOnly = true
                this.radGridView1.CellFormatting += new CellFormattingEventHandler(radGridView1_CellFormatting); 
            } 
     
            void radGridView1_CellFormatting(object sender, CellFormattingEventArgs e) 
            { 
                if (e.CellElement.ColumnInfo.HeaderText == "A"
                { 
                    e.CellElement.Text =( new TimeSpan((int)e.CellElement.RowInfo.Cells[e.CellElement.ColumnIndex].Value)).ToString(); 
                } 
            } 

    Please refer to this help topic about CellFormatting.

    I hope that helps. Do not hesitate to write me back if you have further questions.

    Kind regards,
    Nick
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  5. erwin
    erwin avatar
    358 posts
    Member since:
    Dec 2006

    Posted 24 Aug 2009 Link to this post

         
    // added some checking here to make sure the grid does not crash on unexpected data

    void
     _grid_CellFormatting(object sender, CellFormattingEventArgs e) 
            { 
                if (e.CellElement.ColumnInfo is GridViewDataColumn && !(e.CellElement.RowElement is GridTableHeaderRowElement)) 
                { 
     
                    GridViewDataColumn column = (GridViewDataColumn)e.CellElement.ColumnInfo; 
     
                    if (column.FieldName == "Minutes"
                    { 
                        try 
                        { 
                            int minutes = (int)e.CellElement.RowInfo.Cells["Minutes"].Value; 
                            TimeSpan ts = TimeSpan.FromMinutes(minutes); 
     
                            e.CellElement.Text = string.Format("{0:00}:{1:00}", ts.Hours, ts.Minutes); 
                        } 
                        catch (System.Exception) 
                        { 
                            // error handler goes here 
                        } 
                    } 
                } 
            } 
  6. Jill-Connie Lorentsen
    Jill-Connie Lorentsen avatar
    118 posts
    Member since:
    Nov 2008

    Posted 08 Sep 2009 Link to this post



  7. Nick
    Admin
    Nick avatar
    767 posts

    Posted 11 Sep 2009 Link to this post

    Hello,

    Thank you for your feedback Erwin.

    Sincerely yours,
    Nick
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Back to Top
UI for WinForms is Visual Studio 2017 Ready