Home > Enterprise >  Convert Int column to Datetime in Gridview
Convert Int column to Datetime in Gridview

Time:07-05

I have a column generated as int from datasource, how can I change it to dispay minutes and seconds? Is there a property in Gridview? For example. if I have value 70 to be shows as 01:10 or 1m 10s

CodePudding user response:

I guess you mean enter image description here

so, we have that colum with seconds - we want to convert to HH:mm:ss

So, we can use the row data bound event, and add this code:

    protected void GHotels_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label txtSeconds = e.Row.FindControl("txtSeconds") as Label;
            TimeSpan MySeconds = new TimeSpan(0,0,Convert.ToInt32(txtSeconds.Text));

            txtSeconds.Text = MySeconds.ToString(@"hh\:mm\:ss");
        }
    }

And now we get:

enter image description here

  • Related