显示True False为红色/绿色

时间:2014-04-15 05:47:01

标签: c# asp.net

下面我从数据库中获取“status”数据“true / false”,并将数据添加到名为List1的列表中。我想要实现的是,如果值为true,则单元格的颜色应为红色。我如何实现这一目标?

List<Details> List1 = new List<Detail>();
foreach (var item in xyz)
{
      staus = bsCls.GetStatus(xy,uw,yx);

      if (staus != null)
      {
          List1.Add(staus);//here i need to change
      }
}

if (staus != null)
{
      dates.DataSource = List1;
      dates.DataBind();
      e.Row.Cells[i].Controls.Add(dates);
}

4 个答案:

答案 0 :(得分:0)

你必须找到该网格的单元格并进行如下编码: -

GridView1.Rows[i].Cells[j].BackColor = System.Drawing.Color.LightBlue;

答案 1 :(得分:0)

使用Grid.RowDataBound控件并基于True / False的绑定值,隐藏或显示网格列中的红色图像或蓝色colr图像..看看这是否有帮助http://forums.asp.net/t/1555748.aspx?How+to+display+image+in+gridview+according+to+Condition

答案 2 :(得分:0)

protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
     //check for true/false of datarow column value
     if (Convert.ToString(DataBinder.Eval(e.Row.DataItem, "List1.CloumnName")) == "True")
     {
         e.Row.BackColor = System.Drawing.Color.Red;
     }
     else
     {
         e.Row.BackColor = System.Drawing.Color.Green;
     }
}

答案 3 :(得分:0)

您正尝试向List添加颜色,这是不可能的,一旦您将数据绑定到视图,您需要遍历所有数据项并设置颜色如下。

GridView1.Rows[0].Cells[0].BackColor = status ? System.Drawing.Color.Red : System.Drawing.Color.Green;
相关问题