1.单选一行
设置DataGridView的属性MultiSelect为false
DataGridViewCheckBoxCell列只能勾选一行
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
private void DgvGrid_CellContentClick(object sender, DataGridViewCellEventArgs e) { int count = DgvGrid.Rows.Count; for (int i = 0; i < count; i++) { DataGridViewCheckBoxCell check = (DataGridViewCheckBoxCell)DgvGrid.Rows[i].Cells[0]; Boolean flag = Convert.ToBoolean(check.Value); if (flag == true) { check.Value = false; } else { continue; } } }
2 列值变化立即触发CellValueChanged事件
CellValueChanged事件是在列值变化后触发,若要实现即时触发应同时使用CurrentCellDirtyStateChanged事件;
以下为MSDN代码
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
// This event handler manually raises the CellValueChanged event // by calling the CommitEdit method. 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 == "CheckBoxes") { DataGridViewDisableButtonCell buttonCell = (DataGridViewDisableButtonCell)dataGridView1. Rows[e.RowIndex].Cells["Buttons"]; DataGridViewCheckBoxCell checkCell = (DataGridViewCheckBoxCell)dataGridView1. Rows[e.RowIndex].Cells["CheckBoxes"]; buttonCell.Enabled = !(Boolean)checkCell.Value; dataGridView1.Invalidate(); } }