Click here to Skip to main content
14,391,232 members
Rate this:
Please Sign up or sign in to vote.
I want to change the forecolor of the row which row contains a specific word "MG". I used the below code but it is not working on the web application.

What I have tried:

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
       if (e.Row.RowType == DataControlRowType.DataRow)
       {
           if (e.Row.Cells[0].Text.ToString().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";
           }
      }
}
Posted
Updated 27-Oct-19 12:31pm
v2
Comments
ZurdoDev 25-Oct-19 7:09am
   
Debug it and see if it is even hitting your code the way you want to.
Member 14192879 27-Oct-19 0:43am
   
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.
ZurdoDev 27-Oct-19 10:04am
   
It means that e.row.cells[0] is not what you think it is. So put a breakpoint there and then you can examine what it is and get it fixed.
Gerry Schmitz 25-Oct-19 16:57pm
   
What color is it? What happens when you change "black" to "not black"?
Kornfeld Eliyahu Peter 27-Oct-19 10:27am
   
Check if you actually get the right HTLM code on the client side... (probably not)
I would also advise to use class names instead of inline style...

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

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)
       {
           //if you have a Label control within TemplateField Column
           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";
           }
      }
}
   
Comments
Member 14192879 27-Oct-19 18:07pm
   
thank a lot. now it's working well

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100