簡體   English   中英

如何在ASP.NET網站中根據列條件設置行顏色

[英]How to set row color based on column condition in asp.net website

 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的列,在databind行中,我得到了messageActive的值。 如果messageActive值為“Yes”,則不需要進行任何更改。 如果為“否”,我想以紅色顯示特定行,如何設置行數據綁定中的行背景顏色

網格視圖的某些屬性

   <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; 使用System.Drawing;

我收到了這個錯誤

 '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); 

嘗試這個:

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

在您的方法中添加:

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

作為旁注,我會操縱PreRender事件處理程序中的顏色或其他樣式,而不是RowDataBound ...

編輯:您應該將引用添加到.NET程序集System.Drawing,默認情況下它不包含在ASP.NET Web項目模板中...

您需要更改此行:

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

要明確引用所需的圖像類,例如:

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

或者如果你想使用那個類,可以到另一個類。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM