Select row on DataGridView right click


This was the suggested way to acomplish a row select from a couple sources:

Private Sub dataGrid1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DataGridView1.MouseUp

Dim pt = New Point(e.X, e.Y)

Dim hti As DataGrid.HitTestInfo = DataGridView1.HitTest(pt)

If hti.Type = DataGrid.HitTestType.Cell Then

DataGridView1.CurrentCell = New DataGridCell(hti.Row, hti.Column)

DataGridView1.Select(hti.Row)

End If

End Sub

I found a much easier way:

Private Sub DataGridView1_CellMouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles DataGridView1.CellMouseUp

DataGridView1.CurrentCell = DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex)

DataGridView1.Rows(e.RowIndex).Selected = True

End Sub

Leave a Reply