如何根据datagridview单元格的内容变动设置其单元格的颜色? 

解决方案 »

  1.   


    private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
            {
                if (e.Value != null && e.Value.ToString() == "1")
                {
                    e.CellStyle.BackColor = Color.Red;
                }
            }
      

  2.   

    private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
      {
      if (e.RowIndex == -1)
      return;  DataGridViewRow dgr = dataGridView1.Rows[e.RowIndex];   
      try
      {
      if(e.ColumnIndex==0)
      {
      if (dgr.Cells["txtID"].Value.ToString() == "" )
      {   
      e.CellStyle.BackColor = Color.Red;   
      }
      }  if (e.ColumnIndex == 1)
      {
      if (dgr.Cells["txtName"].Value.ToString() == "")
      {
      e.CellStyle.ForeColor = Color.Red;
      e.CellStyle.BackColor = Color.Green;
      }
      }  }
      catch (Exception ex)
      {
      MessageBox.Show(ex.Message);
      }