Click here to Skip to main content
15,894,362 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
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

VB
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


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.

VB
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
Posted
Comments
Member 16136611 9-Nov-23 16:27pm    
Thank you.

Before you call mouse-right menu, catch datagridview.cellmousedown[^] event and set selected row as current.

More at:
DataGridView Class[^]
DataGridView.CurrentCell[^]
DataGridView Control[^]
 
Share this answer
 
Comments
Zachary.shupp 15-May-12 18:32pm    
could you provide an example. I have been reading the cellmousedown link you have an they dont really explain what to use. Help you would be great.
So after reading several articals from microsoft i have done it. below is the code i used.

VB
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.
 
Share this answer
 
Comments
Maciej Los 16-May-12 0:48am    
It's wonderful to find solution yourself. A 5!
I would be thankful for accepting my answer (formally), because i showed you a way to achieve solution. ;) Please, rate my answer...
Disk4mat73 7-Aug-19 2:48am    
Thank god for you! Ive been searching and trying code snippets for days and your code works perfectly! 5 Stars!

Other snippets Ive tried breaks the row select when a context menu is is set to the datagrid. But yours simply works!
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:

C#
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;
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900