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

Format string for time span?

5 Answers 597 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Jill-Connie Lorentsen
Top achievements
Rank 1
Jill-Connie Lorentsen asked on 21 Aug 2009, 01:19 PM
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

5 Answers, 1 is accepted

Sort by
0
erwin
Top achievements
Rank 1
Veteran
Iron
answered on 23 Aug 2009, 08:19 PM
I used the CellFormatting Event to custom format
timespan values.

Maybe there is a more elegant way?

Regards
Erwin
0
Nick
Telerik team
answered on 24 Aug 2009, 10:01 AM
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.
0
erwin
Top achievements
Rank 1
Veteran
Iron
answered on 24 Aug 2009, 03:19 PM
     
// 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 
                    } 
                } 
            } 
        } 
0
Jill-Connie Lorentsen
Top achievements
Rank 1
answered on 08 Sep 2009, 09:08 AM



0
Nick
Telerik team
answered on 11 Sep 2009, 07:12 AM
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.
Tags
GridView
Asked by
Jill-Connie Lorentsen
Top achievements
Rank 1
Answers by
erwin
Top achievements
Rank 1
Veteran
Iron
Nick
Telerik team
Jill-Connie Lorentsen
Top achievements
Rank 1
Share this question
or