开发者

Double-click DataGridView row?

I am usi开发者_运维问答ng 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).


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.


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();

        }
    }


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


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.


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.


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜