2011-03-10 95 views
0

我的應用程序中有一個Winforms DataGridView。在Windows窗體中捕獲複選框單擊事件DataGridview

我有兩個複選框列以及來自數據庫的5個其他列。這兩個複選框列都添加了DataGridViewCheckBoxColumn。

當用戶點擊第二個複選框時,如果未選中該行的第一個複選框,則需要向用戶顯示一條消息。

我該如何解決這個問題? 我試過了,但單元格值爲空。 我在做什麼錯?

private void dgTest_CellClick(System.Object sender, DataGridViewCellEventArgs e) 
{ 
    DataGridViewCheckBoxCell officialCbCell = row.Cells[1] as DataGridViewCheckBoxCell; 
    DataGridViewCheckBoxCell includeCbCell = row.Cells[0] as DataGridViewCheckBoxCell; 

    if (officialCbCell != null) 
    { 
     if (officialCbCell.Value != null && (bool)officialCbCell.Value == true) 
     { 
      if (includeCbCell != null && (bool)includeCbCell.Value == false) 
      { 
       MessageBox.Show("INVALID"); 
      } 
     } 
    } 
} 

謝謝。

+0

對此有任何意見嗎?謝謝。 – Jimmy 2011-03-10 12:51:21

回答

7

您可以嘗試使用CellValueChanged事件電網

void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e) 
{ 
    if (e.ColumnIndex == 0) 
    { 
     bool isChecked = (Boolean) dataGridView1[0, e.RowIndex].FormattedValue; 

     if (isChecked) 
      dataGridView1[1, e.RowIndex].Value = true; 
    } 
} 

如果選中,那麼你可以設置其他列也檢查或任何其他驗證

1

CellContentClick事件和cell.EditingCellFormattedValue財產也如果您只是取消/單擊單元格,則很有用。

相關問題