Quote:
firstly I tried by using bound field then my code works well for color changing but my main work is with the Template field so when I check with breakpoints. it does not go to if condition and always go to else statement. what to do?. my code is working properly with the bound field but not with a template field.
That's an expected behavior since
e.Row.Cells[0]
refers to
BoundField
columns. When using
TemplateFields
, you need to use
FindControl [
^] method. For example:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string labelValue = ((Label)e.Row.FindControl("YourLabelControlID")).Text;
if (labelValue.Contains("MG"))
{
e.Row.ForeColor = System.Drawing.Color.Blue;
e.Row.Style["text-align"] = "Left";
}
else
{
e.Row.ForeColor = System.Drawing.Color.Black;
e.Row.Style["text-align"] = "Left";
}
}
}