To deselect all rows and cells in a DataGridView , you can use the ClearSelection method :
myDataGridView.ClearSelection()
If you do not want even the first row / cell to be selected, you can set the CurrentCell Nothing / null property , which temporarily hides the focus rectangle until the control gets focus again:
myDataGridView.CurrentCell = Nothing
To determine when the user clicked on the empty part of the DataGridView , you need to handle the MouseUp event. In this case, you can HitTest indicate the location of the click and follow this to indicate HitTestInfo.Nowhere . For example:
Private Sub myDataGridView_MouseUp(ByVal sender as Object, ByVal e as System.Windows.Forms.MouseEventArgs) ''
Of course, you can also subclass an existing DataGridView control to combine all of these functions into one custom control. You need to override the OnMouseUp method, similar to the one shown above. I would also like to provide the public DeselectAll method for convenience, which calls the ClearSelection method and sets the CurrentCell property to Nothing .
(Code samples are arbitrary in VB.NET because the question does not indicate an apology for the language, unless it is your native dialect.)
Cody Gray Nov 30 2018-10-30 14:43
source share