将GridView1绑定到一个DataTable1,DataTable1的结构如下:
产品名称 产量 是否合格
A 30 0
B 20 1
C 39 0
D 40 1
现在需要将是否合格列中值为0的行的背景色设为红色,该怎么做呢?

解决方案 »

  1.   

    先循环查找嵌入DATAGRIDVIEW的数据然后得到索引再根据索引设置cell的背景色或者去看datagridview 有没有条件设置的属性
      

  2.   

    还可以在CellFormating事件里处理 
     private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
            {
                if(e.RowIndex == 0)
                {
                    e.CellStyle.BackColor = Color.Blue;
                }
                else if(e.RowIndex == 1)
                {
                    e.CellStyle.BackColor = Color.Red;
                }
                else 
                {
                    e.CellStyle.BackColor = Color.Green;
                }
            }
    你可以根据你的条件判断哪行需要改变颜色,根据上边颜色设置下。
      

  3.   

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
           if (e.Row.RowType == DataControlRowType.DataRow)
              {
                  string _value = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "列的名字"));
                   if (_value == "0")
                   {
                       e.Row.BackColor = System.Drawing.Color.Red;
                   }
                }
    }
      

  4.   

    在GridView的RowDataBound事件中,如果"是否合格"在第三列,代码如下:        if (e.Row.RowType == DataControlRowType.DataRow)
            {
                if (Convert.ToInt32(e.Row.Cells[2].Text) == 0)
                    e.Row.BackColor = System.Drawing.Color.Red;
            }
      

  5.   

    你可以在dataGridView绑定后判断
    假如你上面的表结构绑定dataGridView后,是否合格是最后一列的话可以:
    for(int i=0;i<dataGridView.Columns.Count;i++)
    {
        if(i!=2);//如果当前列不是【是否合格】这一列則不再进行下面的比较
           continue;
        for(int j=0;j<dataGridView.RowCount;j++)
        {
           if(dataGridView.Rows[j].Cells[i].FormatValue.ToString()=="0")
           {
               dataGridView.Rows[j].Cells[i].CellStyle.BackColor = Color.你想要的颜色;
           }
           
        }
    }