2014-09-11 81 views
0

我有一个dataGridView,他的第一列是checkBox。dataGridView CheckBox检查不工作

我想在此复选框上使用选中的事件。

这是我的代码:

dataGridView1.EditingControlShowing += (sender, e) => 
      {               


       if (dataGridView1.CurrentCell.ColumnIndex == 0) 
       { 

        CheckBox cb = (CheckBox)e.Control; 
        cb.CheckedChanged += (s, e1) => 
         { 
          dosomething(); 
         }; 
        } 
       }; 

,但是当我在第一列改变复选框它从未进入的CheckedChanged事件。

回答

1

则可以将事件更改为CellContentClick事件,而不是,然后检查它是否是您的复选框列:

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) 
{ 
    if (dataGridView1.Columns[e.ColumnIndex] is DataGridViewCheckBoxColumn) 
    { 
     DataGridViewCheckBoxCell cbCell = (DataGridViewCheckBoxCell)dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex]; 

     if (cbCell.Value == cbCell.TrueValue) 
     { 
      cbCell.Value = cbCell.FalseValue; 
     } 
     else 
     { 
      cbCell.Value = cbCell.TrueValue; 
     } 
     } 
}