简体   繁体   English

ASP.NET Gridview 不会更改某些行的背景色

[英]ASP.NET Gridview won't change backcolor for some rows

I am trying to change the colour of the row of a gridview depending on a value in one of the cells.我正在尝试根据其中一个单元格中的值更改 gridview 行的颜色。 However it only partially works.然而,它只是部分工作。 Here is the code:这是代码:

protected void attn_list_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        
        if (e.Row.Cells[4].Text == "P") //change the colour of rows
        {
            e.Row.BackColor = Color.FromName("#CCDDCC");
        }
        else if (e.Row.Cells[4].Text == "L")
        {
            e.Row.BackColor = Color.FromName("#C5DAFC");
        }
        else if (e.Row.Cells[4].Text == "E")
        {
            e.Row.BackColor = Color.FromName("#FFCC66");
        }
        else if (e.Row.Cells[4].Text == "U")
        {
            e.Row.BackColor = Color.FromName("FFCECE");
        }
        else if (e.Row.Cells[4].Text == "N")
        {
            e.Row.BackColor = Color.FromName("EDC9FF");
        }
        if (e.Row.Cells[5].Text == "Yes")
        {
            e.Row.Cells[5].ForeColor = Color.FromKnownColor(KnownColor.Red); //if EL, set colour of font as red
        }
    }

And the outcome:结果: 结果

I tried to test the conditions by putting in System.Diagnostic.Debug.Writeline("A") into the else if (e.Row.Cells[4].Text=="N") statement, and turns out it printed "A", meaning it actually met the conditions.我试图通过将System.Diagnostic.Debug.Writeline("A")放入else if (e.Row.Cells[4].Text=="N")语句来测试条件,结果它打印了“ A”,表示它实际上满足了条件。 Just that I don't know why the colour won't get changed...只是不知道为什么颜色不会改变......

Thanks in advance提前致谢

Well, the more code, the more we glance over it!好吧,代码越多,我们浏览的次数就越多!

so, converting above to this:因此,将上面转换为:

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            string sColor = "";
            switch(e.Row.Cells[4].Text)
            {
              case "P": sColor = "#CCDDCC"; break;
              case "L": sColor = "#C5DAFC"; break;
              case "E": sColor = "#FFCC66"; break;
              case "U": sColor = "FFCC66"; break;
              case "N": sColor = "EDC9FF"; break;
            }
            if (sColor != "")
                e.Row.BackColor = Color.FromName(sColor);

            if (e.Row.Cells[5].Text == "Yes")
            {
                e.Row.Cells[5].ForeColor = Color.FromKnownColor(KnownColor.Red); //if EL, set colour of font as red
            }
        }

Hum, sure looks like you missing the "#" for some of your colors.嗯,看起来您确实缺少某些颜色的“#”。

Also, note how we check if data bound is actually procssing a data row, the header and other types of "rows" are tossed to that routine, so we should (need) a check for the row type also.另外,请注意我们如何检查数据绑定是否实际上正在处理数据行,标题和其他类型的“行”被扔到该例程中,因此我们也应该(需要)检查行类型。

but, so far, it looks like the "# is missing.但是,到目前为止,似乎缺少“#”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM