如何在asp.net网站中根据列条件设置行颜色

发布于 2024-12-11 20:25:29 字数 2101 浏览 0 评论 0原文

 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

怪异←思 2024-12-18 20:25:29

试试这个:

string status = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "MessageActive"));
if (status == "No")
{
    e.Row.BackColor = Drawing.Color.Red
}

Try this:

string status = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "MessageActive"));
if (status == "No")
{
    e.Row.BackColor = Drawing.Color.Red
}
抚笙 2024-12-18 20:25:29

在您的方法中添加此内容:

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

作为旁注,我将在 PreRender 事件处理程序中操作颜色或其他样式,而不是在 RowDataBound 中操作...

编辑: 您应该添加对 .NET 程序集 System.Drawing 的引用,因为默认情况下它不包含在 ASP.NET Web 项目模板中...

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...

反话 2024-12-18 20:25:29

您需要更改这一行:

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

要明确引用您想要的图像类,例如:

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

或者如果您想使用另一个类,则更改为另一个类。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文