Reputation: 1123
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
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