I adapted code from this question how to select rows on cellclick, and also columns on column header click? into a single DataGridView1_MouseDown event, because it wasn't allowing me to select multiple rows/columns using the "Ctrl" key.
What I'd like, is to be able to select multiple rows (clicking on the row indices) OR multiple columns (clicking on the column headers) by selecting one after another keeping "Ctrl" pressed. I can easily get one OR the other (setting DataGridViewSelectionMode to either FullRowSelect or ColumnHeaderSelect) and then Ctrl works, but I'd like to have both functionality on the same DataGridView.
I feel like I'm so close. Thanks for any tips!
Private Sub DataGridView1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DataGridView1.MouseDown
Dim ht As DataGridView.HitTestInfo
ht = Me.DataGridView1.HitTest(e.X, e.Y)
If e.Button = Windows.Forms.MouseButtons.Left Then
If ht.Type = DataGridViewHitTestType.Cell Then
DataGridView1.SelectionMode = DataGridViewSelectionMode.CellSelect
DataGridView1.CurrentCell.Selected = True
ElseIf ht.Type = DataGridViewHitTestType.RowHeader Then
DataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect
DataGridView1.Rows(ht.RowIndex).Selected = True
ElseIf ht.Type = DataGridViewHitTestType.ColumnHeader Then
DataGridView1.SelectionMode = DataGridViewSelectionMode.ColumnHeaderSelect
DataGridView1.Columns(ht.ColumnIndex).Selected = True
End If
End If
End Sub