在datagridview中单击鼠标右键 [英] right click in a datagridview

查看:159
本文介绍了在datagridview中单击鼠标右键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个应用程序,它将查询文件,并且那里的数据将被添加到工作得很好的数据网格中.我的问题是上下文菜单.首先,我必须左键单击该项目,然后在选择它后右键单击它,并修复我添加的内容

i have this application that will query a file and the data that is there will be added the the data grid which works just fine. the issue im having is with the context menu. at first i had to left click the item then right click it after it was selected and to fix that i added

Private Sub CAE_DGV_CellMouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles CAE_DGV.CellMouseDown
    'makes it so when you right click on the DGV it will select the row your clicking on.
    If e.Button = MouseButtons.Right Then
        CAE_DGV.ClearSelection()
        CAE_DGV.Rows(e.RowIndex).Selected = True
    End If

End Sub



现在,在上下文菜单中,根据所选行中的数据,我需要将某些项目显示为灰色,这就是因为如果我在菜单甚至显示之前右键单击某个项目,我仍然会遇到问题,因为出现错误:对象引用未设置为对象的实例".我在以下代码中的i = CAE_DGV.CurrentRow.Index处收到此错误.如果有人可以向我指出如何纠正错误的正确方向,然后右键单击选择一个项目,然后打开上下文菜单,帮助将非常有用.



Now in the context menu there are some items i need grayed out based on the data in the row selected and thats where im still having the issue becasue if i right click on an item before the menu even shows up i get the error: "Object reference not set to an instance of an object". i get this error at i = CAE_DGV.CurrentRow.Index in the below code. Help would be great if someone could point me in the right direction on how to correct the error and have the right click select an item and then open the context menu.

Private Sub CAE_ContextMenuStrip1_Opening(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles CAE_ContextMenuStrip1.Opening

    Dim i As Integer
    Dim AppVERIFY

    i = CAE_DGV.CurrentRow.Index
    AppVERIFY = CAE_DGV.Item(5, i).Value ' verify.

    If AppVERIFY = "" Or AppVERIFY = "_NONE_" Then
        VerifyToolStripMenuItem.Visible = False
    End If

    If Application.HPCAE_status.ForeColor = Color.Red Then
        VerifyToolStripMenuItem.Enabled = False
        DeleteHeapFolderToolStripMenuItem.Enabled = False
    End If

    If Application.HPCAE_status.ForeColor = Color.Green Then
        VerifyToolStripMenuItem.Enabled = True
        DeleteHeapFolderToolStripMenuItem.Enabled = True
    End If
End Sub

推荐答案

在调用鼠标右键菜单之前,先捕获 datagridview.cellmousedown [ ^ ]事件并将所选行设置为当前行.

更多信息:
DataGridView类 [ DataGridView.CurrentCell [ DataGridView控件 [
Before you call mouse-right menu, catch datagridview.cellmousedown[^] event and set selected row as current.

More at:
DataGridView Class[^]
DataGridView.CurrentCell[^]
DataGridView Control[^]


因此,从Microsoft阅读了几篇文章之后,我就完成了.以下是我使用的代码.

So after reading several articals from microsoft i have done it. below is the code i used.

Private Sub DataGridView1_CellMouseDown(sender As Object, e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles DataGridView1.CellMouseDown
    If e.Button = MouseButtons.Right Then
        DataGridView1.CurrentCell = DataGridView1(e.ColumnIndex, e.RowIndex)
    End If
End Sub



因为我在属性中分配了上下文菜单,所以我真正要做的就是使鼠标右键的工作方式与
鼠标左键.我确信其他人会做不同的事情,但这对我有用.



since i assigned the context menu in the properties all i really did was make the right mouse button work the same way as the
left mouse button. I m sure others would do it differently but this works for me.


如果您将BindingSource用作DataSource,最好从模型中选择,以便模型更新视图,否则,如果您查询BindingSource的当前位置,则该位置是先前选择的项目,而不是您右键单击的项目

像这样的东西:

In the case you are using a BindingSource as DataSource, is best to select from the model so the model updates the view, otherwise if you query the current of the BindingSource at thsi point is the previously selected item, not the one you rigth-click

Something like this:

private void dataGridView_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
    if (e.Button == MouseButtons.Right)
    {
        int rowSelected = e.RowIndex;

        if (e.RowIndex != -1)
            bindingSource.Position = e.RowIndex;
    }
}


这篇关于在datagridview中单击鼠标右键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆