Alex Alex - 5 months ago 41
Vb.net Question

Double-click DataGridView row?

I am using vb.net and DataGridView on a winform.

When a user double-clicks on a row I want to do something with this row. But how can I know whether user clicked on a row or just anywhere in the grid? If I use

DataGridView.CurrentRow
then if a row is selected and user clicked anywhere on the grid the current row will show the selected and not where the user clicked (which in this case would be not on a row and I would want to ignore it).

Answer

Try the CellMouseDoubleClick event...

Private Sub DataGridView1_CellMouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles DataGridView1.CellMouseDoubleClick
    If e.RowIndex >= 0 AndAlso e.ColumnIndex >= 0 Then
        Dim selectedRow = DataGridView1.Rows(e.RowIndex)
    End If
End Sub

This will only fire if the user is actually over a cell in the grid. The If check filters out double clicks on the row selectors and headers.