vb.net Datagridview - 专注于右键单击的单元格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2524221/
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 14:48:19  来源:igfitidea点击:

Datagridview - Focus on Cell that was right clicked

vb.netdatagridviewclickedit

提问by ErocM

I have a datagridview that I have put a ContextMenuStrip1 on. I would like it to remove a row in the datagridview when the row is right clicked on and they click on "delete row". I have the delete working and the menu is showing up but this isn't firing when you right click on the datagridview.

我有一个 datagridview,我已经把 ContextMenuStrip1 放在上面。当右键单击该行并单击“删除行”时,我希望它删除 datagridview 中的一行。我有删除工作并且显示菜单但是当您右键单击数据网格视图时这不会触发。

This is where I am setting the row to edit:

这是我设置要编辑的行的地方:

   Private Sub ModifyRowToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ModifyRowToolStripMenuItem.Click
    If Not datagridview_TagAssignment.CurrentRow Is Nothing Then
      datagridview_TagAssignment.CurrentCell = datagridview_TagAssignment.Item(0, datagridview_TagAssignment.CurrentRow.Index)
      datagridview_TagAssignment.BeginEdit(True)
    End If
  End Sub

I am always ending up on row(0) and never the row I right clicked on.

我总是在第(0)行结束,而不是我右键单击的行。

 Private Sub datagridview_TagAssignment_CellMouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles datagridview_TagAssignment.CellMouseClick
    If e.Button = Windows.Forms.MouseButtons.Right AndAlso e.RowIndex >= 0 Then
      datagridview_TagAssignment.Rows(e.RowIndex).Selected = True
    End If
  End Sub

Anyone have any suggestions?

有人有什么建议吗?

回答by msarchet

Private Sub DataGridView1_MouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DataGridView1.MouseClick
    If e.Button = Windows.Forms.MouseButtons.Right Then
        rowClicked = DataGridView1.HitTest(e.Location.X, e.Location.Y).RowIndex
        ContextMenuStrip1.Items.Add(rowClicked.ToString)
        ContextMenuStrip1.Show(DataGridView1, e.Location)
        ContextMenuStrip1.Items.Clear()
    End If
End Sub

Edit: Updated to handle a context menu strip.

编辑:更新以处理上下文菜单条。

That should give you the row index of the row that was right clicked using the mouse coordinates. Which should let you delete the row based on knowing the index.

这应该为您提供使用鼠标坐标右键单击的行的行索引。这应该让您根据知道索引删除行。

Edit

编辑

Per Your comment on it not working this is my code

根据您对它的评论不起作用,这是我的代码

I have a Solution with a WinForm with a dataGridView added to it. and this is the code in the form.

我有一个带有 WinForm 的解决方案,其中添加了 dataGridView。这是表格中的代码。

Public Class Form1

    Dim bindS As New BindingSource
    Dim rowClicked As Integer
    Private Sub DataGridView1_MouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DataGridView1.MouseClick
        If e.Button = Windows.Forms.MouseButtons.Right Then
            rowClicked = DataGridView1.HitTest(e.Location.X, e.Location.Y).RowIndex
            ContextMenuStrip1.Items.Add(rowClicked.ToString)
            ContextMenuStrip1.Show(DataGridView1, e.Location)
            ContextMenuStrip1.Items.Clear()
        End If

    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim s As New List(Of String)
        s.Add("String one")
        s.Add("String Two")
        bindS.DataSource = s
        DataGridView1.DataSource = bindS


    End Sub
End Class

Right Clicking on a row shows the correct row index

右键单击一行显示正确的行索引

Make sure that the event args that you are handling are the System.Windows.Forms.MouseEventArgsI noticed that you're handling the cell click

确保您正在处理的事件参数是System.Windows.Forms.MouseEventArgs我注意到您正在处理单元格单击