如何在asp.net网站中根据列条件设置行颜色
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";
}
}
}
我有一个 gridView,它有一个名为 MessageActive 的列,在行数据绑定中我获取了 messageActive 的值。如果 messageActive 值为“是”,则无需进行任何更改。如果是“否”我想以红色显示特定行,如何在行数据绑定中设置行背景颜色
网格视图
<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" />
命名空间 的一些属性 使用 System.Web.UI.WebControls; 使用系统绘图;
我收到这个错误
'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);
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);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
试试这个:
Try this:
在您的方法中添加此内容:
作为旁注,我将在
PreRender
事件处理程序中操作颜色或其他样式,而不是在RowDataBound
中操作...编辑: 您应该添加对 .NET 程序集 System.Drawing 的引用,因为默认情况下它不包含在 ASP.NET Web 项目模板中...
add this in your method:
as side note, I would manipulate the color or other styles in the
PreRender
event handler and not in theRowDataBound
...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...
您需要更改这一行:
要明确引用您想要的图像类,例如:
或者如果您想使用另一个类,则更改为另一个类。
You need to change this line:
To explictily reference the image class you want so for example:
Or to the other class if you want to use that one.