1
 protected void MyGrid_RowDataBound(object sender, GridViewRowEventArgs e)
    {
    if (e.Row.RowType == DataControlRowType.DataRow)
        {
        Image img = (Image)e.Row.FindControl("Status");
            int msgid;

            int.TryParse(Convert.ToString(DataBinder.Eval(e.Row.DataItem, "MsgID")), out msgid);            
            string status = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "MessageActive"));
        if(status.Equals("No"))
        {
            e.Row.BackColor = Color.Red;
        } 
         //Based on some condition I am assigning images to the row
           if (value >= Toptarg)
            {
                img.ImageUrl = "Styles/Images/GreenBox.jpg";
                img.ToolTip = "Met Top Target";
                img.AlternateText = "Met Top Target";
            }
            else
            {
                img.ImageUrl = "Styles/Images/AmberBox.jpg";
                img.ToolTip = "In Progress";
                img.AlternateText = "In Progress";
            }

         }
      }

I have a gridView and it has a column named MessageActive, In the row databind I get the value of the messageActive. If the messageActive value is 'Yes' no changes are required. If it is 'No' I want to display the particular row in red color, How can I set the row background Color in row databound

Some properties of the grid view

   <RowStyle BackColor="White" />
   <AlternatingRowStyle BackColor="MistyRose" />
   <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
   <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
   <HeaderStyle BackColor="#696969" Font-Bold="True" ForeColor="White" />

Namespace using System.Web.UI.WebControls; using System.Drawing;

I am getting this error

 'Image' is an ambiguous reference between 'System.Web.UI.WebControls.Image' and 'System.Drawing.Image'

Source Error:


Line 56:         if (e.Row.RowType == DataControlRowType.DataRow)
Line 57:         {
Line 58:             Image img = (Image)e.Row.FindControl("Status");
Line 59:             int msgid;
Line 60:             int.TryParse(Convert.ToString(DataBinder.Eval(e.Row.DataItem, "MsgID")), out msgid); 
1
  • 1
    I solved my error by using System.Web.UI.WebControls.Image img = (System.Web.UI.WebControls.Image)e.Row.FindControl("Status");
    – Mark
    Oct 24, 2011 at 12:02

3 Answers 3

1

Try this:

string status = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "MessageActive"));
if (status == "No")
{
    e.Row.BackColor = Drawing.Color.Red
}
2
  • I did the same thing it, Drawing does not exist in current context error. I included using System.Drawing; but still same error
    – Mark
    Oct 24, 2011 at 11:43
  • @Mark: You probably need an additional reference to System.Drawing.Color in your project. Oct 24, 2011 at 11:49
1

add this in your method:

if(status.Equal("No"))
{
  e.Row.BackColor = Color.Red; // or Color.FromName("#FF0000");
}

as side note, I would manipulate the color or other styles in the PreRender event handler and not in the RowDataBound...

Edit: You should add the reference to the .NET assembly System.Drawing as by default it is not included in the ASP.NET web project templates...

0
1

You need to change this line:

Image img = (Image)e.Row.FindControl("Status");

To explictily reference the image class you want so for example:

System.Web.UI.WebControls.Image img = (System.Web.UI.WebControls.Image)e.Row.FindControl("Status");

Or to the other class if you want to use that one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.