I need to put a Checkbox against each Row in开发者_高级运维 a DataGridView. I am making use of DataGridViewCheckBoxColumn for the purpose. But here user is able to select more than 1 row. How to restrict the user from selecting multiple rows in a DataGridView?
You will have to do it yourself when the checkbox is checked.
If you want to respond immediately when users click a check box cell, you can handle the DataGridView.CellContentClick event, but this event occurs before the cell value is updated. If you need the new value at the time of the click, one option is to calculate what the expected value will be based on the current value. Another approach is to commit the change immediately, and handle the DataGridView.CellValueChanged event to respond to it. To commit the change when the cell is clicked, you must handle the DataGridView.CurrentCellDirtyStateChanged event. In the handler, if the current cell is a check box cell, call the DataGridView.CommitEdit method and pass in the Commit value.
Whichever method you use, when you handle the checkbox being checked, you will need to mark all the other checkboxes as unchecked. How you will do that depends on whether (and how) your DataGridView is data-bound.
精彩评论