Reputation: 141
I have a dynamic gridview and formatting needs to be applied for certain columns.
Code:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
for (int i = 0; i < GridView1.HeaderRow.Cells.Count; i++)
{
string strHeader = GridView1.HeaderRow.Cells[i].Text;
List<string> lstCurrency = new List<string>() { "Price", "Amount" };
bool checkAmount = lstCurrency.Exists(o => strHeader.Equals(o));
if (checkAmount)
{
e.Row.Cells[i].Text = String.Format("{0:C2}", e.Row.Cells[i].Text);
}
}
}
}
If gridview column header text contains Price
or Amount
then currency formatting needs to be applied and the above code doesn't work.
Please correct me if I'm doing something wrong.
Upvotes: 0
Views: 1508
Reputation: 2062
You need to convert string e.Row.Cells[i].Text
into decimal
:
e.Row.Cells[i].Text = String.Format("{0:C2}", Convert.ToDecimal(e.Row.Cells[i].Text));
Upvotes: 2
Reputation: 538
Have you tried parsing the value? Use Int or Decimal as per your requirement.
e.Row.Cells[i].Text = String.Format("{0:C2}", Int32.Parse(e.Row.Cells[i].Text));
Upvotes: 1