2014-09-11 83 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; 
     } 
     } 
}