显然我可以循环一次,对我需要的行设置颜色,没搜索到合适方法。我希望能有一种方法,我设置一个委托。每次添加行的时候,自动的检查是否符合条件,我就可以只设置条件了。说的明白一点,有一列,表示时间,我希望所有的周日能淡蓝色显示。除了我要自己循环来实现,有没有DataGridView的事件可以更好的实现?

解决方案 »

  1.   

    CellFormatting事件可以
    private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
            {
                //周日的条件判断
                e.CellStyle.BackColor = Color.LightBlue;
            }
      

  2.   

    在CellFormatting事件中处理        private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
            {
                if (dataGridView1.Rows[e.RowIndex].Cells[0].Value == 1)
                {
                }
                else
                {
                }
            }
      

  3.   

    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() == "1002" )
      {   
      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);
      }