How to deselect all selected rows in a DataGridView control?

I want to deselect all selected rows in a DataGridView control when the user clicks on the empty (non-line) part of the control.
How can i do this?

+47
winforms datagridview
Nov 30 '10 at 14:16
source share
6 answers

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) ''# See if the left mouse button was clicked If e.Button = MouseButtons.Left Then ''# Check the HitTest information for this click location If myDataGridView.HitTest(eX, eY) = DataGridView.HitTestInfo.Nowhere Then myDataGridView.ClearSelection() myDataGridView.CurrentCell = Nothing End If End If End Sub 

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.)

+88
Nov 30 2018-10-30
source share
— -

Thanks to Cody heres C # for the link:

 if (e.Button == System.Windows.Forms.MouseButtons.Left) { DataGridView.HitTestInfo hit = dgv_track.HitTest(eX, eY); if (hit.Type == DataGridViewHitTestType.None) { dgv_track.ClearSelection(); dgv_track.CurrentCell = null; } } 
+6
Nov 30 '10 at 17:17
source share

Set

 dgv.CurrentCell = null; 

when the user clicks on the empty part of dgv.

+1
Nov 30 2018-10-30
source share

I found out why my first line was selected by default, and figured out how not to select it by default.

By default, my datagridview was an object with the first stop tab in my window form. Move the tab first to another object (maybe disabling tabstop for datagrid will work at all) disable first row selection

+1
Jul 21 '13 at
source share

I ran into the same problem and found a solution (not completely myself, but there is internet)

 Color blue = ColorTranslator.FromHtml("#CCFFFF"); Color red = ColorTranslator.FromHtml("#FFCCFF"); Color letters = Color.Black; foreach (DataGridViewRow r in datagridIncome.Rows) { if (r.Cells[5].Value.ToString().Contains("1")) { r.DefaultCellStyle.BackColor = blue; r.DefaultCellStyle.SelectionBackColor = blue; r.DefaultCellStyle.SelectionForeColor = letters; } else { r.DefaultCellStyle.BackColor = red; r.DefaultCellStyle.SelectionBackColor = red; r.DefaultCellStyle.SelectionForeColor = letters; } } 

This is a little trick, the only way you can see that the row is selected is the very first column (not column [0], but one of them). When you press another line, you will no longer see the blue selection, only the arrow indicates which line is selected. As you understand, I use rowSelection in my gridview.

0
Jul 19 '13 at 22:04
source share

In VB.net, use for Sub:

  Private Sub dgv_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles dgv.MouseUp ' deselezionare se click su vuoto If e.Button = MouseButtons.Left Then ' Check the HitTest information for this click location If Equals(dgv.HitTest(eX, eY), DataGridView.HitTestInfo.Nowhere) Then dgv.ClearSelection() dgv.CurrentCell = Nothing End If End If End Sub 
0
Dec 14 '14 at 13:39
source share



All Articles