Reputation: 2111
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).
Upvotes: 15
Views: 90482
Reputation: 9
You can try this:
Private Sub grdview_CellDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles grdview.CellDoubleClick
For index As Integer = 0 To grdview.Rows.Count - 1
If e.RowIndex = index AndAlso e.ColumnIndex = 1 AndAlso grdview.Rows(index).Cells(1).Value = "" Then
MsgBox("Double Click Message")
End If
Next
End Sub
Upvotes: 0
Reputation:
Use Datagridview DoubleClick Evenet and then Datagrdiview1.selectedrows[0].cell["CellName"] to get value and process.
Below example shows clients record upon double click on selected row.
private void dgvClientsUsage_DoubleClick(object sender, EventArgs e) {
if (dgvClientsUsage.SelectedRows.Count < 1)
{
MessageBox.Show("Please select a client");
return;
}
else
{
string clientName = dgvClientsUsage.SelectedRows[0].Cells["ClientName"].Value.ToString();
// show selected client Details
ClientDetails clients = new ClientDetails(clientName);
clients.ShowDialog();
}
}
Upvotes: 4
Reputation: 11358
Use DataGridView.HitTest in the double-click handler to find out where the click happened.
Upvotes: 2
Reputation: 12938
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.
Upvotes: 40
Reputation: 5605
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.
Upvotes: 0
Reputation: 5439
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.
Upvotes: 0