Robert_Junior
Robert_Junior

Reputation: 1123

Formatting dynamically created column

I have a asp grid contents generated dynamically using a sql procedure.
using the procedure datatable returned is binded to the grid. In this grid i have a cost column which I like to round to 4 decimal places(12.1234). How to do the same?

I tried in RowDataBound as

if (e.Row.RowType == DataControlRowType.DataRow)
{
   e.Row.Cells[4].Text = string.Format("{0:N4}", e.Row.Cells[4].Text);
}

But this has no effect. I cannot apply the formatting to datatable as I am using this for cost calculations so rounding the cost could cause problems.

Any help will be appreciated

Upvotes: 1

Views: 206

Answers (1)

Enrique Zavaleta
Enrique Zavaleta

Reputation: 2098

You should convert your variable type from string to decimal/double type and then apply your format:

if (e.Row.RowType == DataControlRowType.DataRow)
{
   e.Row.Cells[4].Text = Convert.ToDecimal(e.Row.Cells[4].Text).ToString("#.####");
}

Upvotes: 2

Related Questions