vb.net 双击 DataGridView 行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3920339/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 15:04:23  来源:igfitidea点击:

Double-click DataGridView row?

vb.netwinformsdatagridviewdouble-click

提问by Alex

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

我在 winform 上使用 vb.net 和 DataGridView。

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

当用户双击一行时,我想对这一行做一些事情。但是我怎么知道用户是点击了一行还是只是在网格中的任何地方?如果我使用,DataGridView.CurrentRow那么如果选择了一行并且用户单击了网格上的任意位置,当前行将显示选定的而不是用户单击的位置(在这种情况下不会在一行上,我想忽略它)。

回答by codeConcussion

Try the CellMouseDoubleClickevent...

试试CellMouseDoubleClick活动...

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 Ifcheck filters out double clicks on the row selectors and headers.

这只会在用户实际位于网格中的单元格上方时触发。该If检查筛选出的行选择和头双击。

回答by Alex

Use Datagridview DoubleClick Evenet and then Datagrdiview1.selectedrows[0].cell["CellName"] to get value and process.

使用 Datagridview DoubleClick Eventet 和 Datagrdiview1.selectedrows[0].cell["CellName"] 获取值并进行处理。

Below example shows clients record upon double click on selected row.

下面的示例显示了双击选定行时的客户记录。

private void dgvClientsUsage_DoubleClick(object sender, EventArgs e) {

私有无效 dgvClientsUsage_DoubleClick(对象发送者,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();

        }
    }

回答by liggett78

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

在双击处理程序中使用DataGridView.HitTest找出单击发生的位置。

回答by Sudharsan

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

回答by davisoa

I would use the DoubleClickevent 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.

我会使用DataGridViewDoubleClick事件。这至少只会在用户双击数据网格时触发 - 您可以使用 MousePosition 来确定用户双击了哪一行(如果有)。

回答by giodamelio

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.

请记住,我无法对此进行测试,因为我没有任何数据来填充我的 DataGridView。