在winform中使用checbox很多。上次那個項目里就用到了,寫了一個不太好用,后來翻閱了一下微軟提供的樣碼,我覺得有必要給大家分享一下。
// This event handler manually raises the CellValueChanged event // by calling the CommitEdit method. public void DataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e) { if (DataGridView1.IsCurrentCellDirty) { DataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit); } } // If a check box cell is clicked, this event handler disables // or enables the button in the same row as the clicked cell. public void DataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e) { if (DataGridView1.Columns[e.ColumnIndex].Name == "CheckBoxs") { DataGridViewCheckBoxCell checkCell = (DataGridViewCheckBoxCell)DataGridView1. Rows[e.RowIndex].Cells["CheckBoxs"]; if ((Boolean)checkCell.Value) { //處理選中
//do something } DataGridView1.Invalidate(); } }