Double-click DataGridView row?

孤人 提交于 2020-01-29 04:36:24

问题


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


回答1:


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.




回答2:


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

        }
    }



回答3:


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




回答4:


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.




回答5:


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.




回答6:


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


来源:https://stackoverflow.com/questions/3920339/double-click-datagridview-row

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!