对DataGridView返回正确索引的单元格索引正确吗?

时间:2019-04-18 13:25:37

标签: vb.net events datagridview cell

我将ContextMenuStrip附加到DataGridView,但是它将执行的操作基于用户右键单击所在的行。

我能够从左键单击中检索坐标,但是没有称为CellRightClick的事件,只有CellClick或CellMouseClick ...等。 但是我在网上阅读了可以跟踪单元位置I / O的信息,但是我希望有一个更简单的解决方案,因为下一步是可移植到触摸屏系统。

编辑:我很幸运地发现,当我右键单击并弹出ContextMenu时,它也被视为像CellMouseLeave事件,所以:

Private Sub t(Sender As Object, E As DataGridViewCellEventArgs) Handles DataGridView1.CellMouseLeave

    LastRightClickedRowIndex = E.RowIndex

End Sub

可能会工作吗?你怎么看?牢固吗?

1 个答案:

答案 0 :(得分:0)

您可以使用DataGridView CellContextMenuStripNeeded事件,该事件在需要单元格的快捷菜单时发生,因为用户右键单击单元格。

Private Sub DataGridView1_CellContextMenuStripNeeded(
    sender As Object, 
    e As System.Windows.Forms.DataGridViewCellContextMenuStripNeededEventArgs) 
    Handles DataGridView1.CellContextMenuStripNeeded

    MsgBox("You click row " & e.RowIndex & " / column " & e.ColumnIndex & "!")

End Sub