为什么gridview单元格的背景颜色没有设置?

时间:2015-10-09 21:50:28

标签: c# asp.net gridview

我把这段代码设置为gridview CELL的颜色,如果不是空的话。确实如此,但整个色谱柱不是特定的细胞。为什么?

 private void ShowCases()
    {
        short UserID = Convert.ToInt16(Session["UserID"]);

        grdviewCases.DataSource = MngCases.SelectCases(UserID);
        grdviewCases.DataBind();

        foreach (GridViewRow gr in grdviewCases.Rows) 
        {
            if (gr.Cells[14].Text != "")
            {
                gr.Cells[14].BackColor= Color.IndianRed;
            }
        }

    }

简而言之:如果单元格不为空,我想将背景颜色设置为红色,但是此代码已经绘制了#14列的所有单元格,空白和非空白。

1 个答案:

答案 0 :(得分:1)

可能是因为您的“空”单元格实际上包含 

如果您将代码更改为,

 if (gr.Cells[14].Text != " ")
 {
     gr.Cells[14].BackColor= Color.IndianRed;
 }

它应该工作: enter image description here

有时,使用调试器非常非常有用......:O)

相关问题