views:

57

answers:

4

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

A: 

I would use the DoubleClick event of the DataGridView. This will at least only fire when the user double clicks in the data grid - you can use the MousePosition to determine what row (if any) the user double clicked on.

davisoa
I am using the DoubleClick of the DataGridView. But how can I using the moustposition to infer if the double click was on an actual row?
bochur1
A: 

You could try something like this.

Private Sub DataGridView1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGridView1.DoubleClick
    For index As Integer = 0 To DataGridView1.Rows.Count
        If DataGridView1.Rows(index).Selected = True Then
            'it is selected
        Else
            'is is not selected
        End If
    Next
End Sub

Keep in mind i could not test this because i diddent have any data to populate my DataGridView.

giodamelio
+2  A: 

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.

whatknott
A: 

Use DataGridView.HitTest in the double-click handler to find out where the click happened.

liggett78