Reputation: 2760
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);
Upvotes: 1
Views: 2544
Reputation: 5303
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.
Upvotes: 1
Reputation: 44595
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...
Upvotes: 1
Reputation: 14387
Try this:
string status = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "MessageActive"));
if (status == "No")
{
e.Row.BackColor = Drawing.Color.Red
}
Upvotes: 1