I’d like to deselect all selected rows in a DataGridView control when the user clicks on a blank (non-row) part of the control.
How can I do this?
I’d like to deselect all selected rows in a DataGridView control when the user
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
To deselect all rows and cells in a
DataGridView, you can use theClearSelectionmethod:If you don’t want even the first row/cell to appear selected, you can set the
CurrentCellproperty toNothing/null, which will temporarily hide the focus rectangle until the control receives focus again:To determine when the user has clicked on a blank part of the
DataGridView, you’re going to have to handle itsMouseUpevent. In that event, you canHitTestthe click location and watch for this to indicateHitTestInfo.Nowhere. For example:Of course, you could also subclass the existing
DataGridViewcontrol to combine all of this functionality into a single custom control. You’ll need to override itsOnMouseUpmethod similar to the way shown above. I also like to provide a publicDeselectAllmethod for convenience that both calls theClearSelectionmethod and sets theCurrentCellproperty toNothing.(Code samples are all arbitrarily in VB.NET because the question doesn’t specify a language—apologies if this is not your native dialect.)