vignesh t

vignesh t

  • NA
  • 63
  • 19.9k

Set currency for dynamic gridview cells in asp.net c#

Jan 12 2018 2:12 AM
Hi,
 
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 lstCurrency = new List() { "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.


Answers (4)