Link to home
Start Free TrialLog in
Avatar of Ross Edwards
Ross EdwardsFlag for United Kingdom of Great Britain and Northern Ireland

asked on

ContextMenuStrip with DataGridView problem

I am working in VN2008.  I have a DGV in a Windows Form.  I have created and bound a ContextMenuStrip object to the DGV, which appears on right-click in the DGV.

My context menu contains only one item ("Flag for action")

I want to call a SQL query and pass a parameter from the selected row in the DGV when the item is selected from the context menu.

I would normally use e.rowindex etc... but this information is not contained in the events for ToolStripMenuItem.Click.  How can I identify which row was selected when the context menu was invoked?

I can think of setting a global variable when the right-click is captured and using this but seems very messy.  There must be a proper way of doing this.

Many thanks in advance!
Avatar of Jorge Paulino
Jorge Paulino
Flag of Portugal image

>> I can think of setting a global variable when the right-click is captured and using this but seems very messy
Not a global but a private variable solves that
Avatar of Ross Edwards

ASKER

How?
You can remove the contextmenu from the properties and do this way:
 

    Private Sub DataGridView1_CellMouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles DataGridView1.CellMouseUp
        If e.Button = Windows.Forms.MouseButtons.Right Then
 
            ' Read information using e.ColumnIndex and e.RowIndex
 
            ' Display the contextmenustrip
            Dim pt As Point = DataGridView1.PointToClient(Control.MousePosition)
            Me.ContextMenuStrip1.Show(Me.DataGridView1, pt)
 
        End If
    End Sub

Open in new window

Thanks

I already have this code and it works fine.  The problem is in the next procedure, which handles ContextMenuStrip1Item1.clicked.

This separate private procedure needs to know which e.columnindex and rowindex was selected in the previous procedure.

This is my problem!
Can you show the code you have ?
I just realised I dont need to call contextmenustrip1.show as it shows automatically as long as it is already bound to the DGV.  Here is my code:
 Private Sub QryListBackordersDataGridView_MouseUp(ByVal sender As Object, ByVal e As MouseEventArgs) Handles QryListBackordersDataGridView.MouseUp
 
        If e.Button = Windows.Forms.MouseButtons.Right Then
                       orderid = QryListBackordersDataGridView.Rows(e.RowIndex).Cells(0).Value.ToString
           ContextMenuStrip1.Show(QryListBackordersDataGridView, e.Location)
 
        End If
         End Sub
 
    Private Sub flagselected() Handles FlagToolStripMenuItem.Click
        ' DO SQL procedure using orderid from previous proc
   End Sub

Open in new window

But you have the information on a variable ?
Private Sub flagselected() Handles FlagToolStripMenuItem.Click
       MessageBox.Show(orderid)
End Sub
I know - orderid is a global variable.  I would rather not use it.  Hence my question - is there a better way of doing it?  
ASKER CERTIFIED SOLUTION
Avatar of Jorge Paulino
Jorge Paulino
Flag of Portugal image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Good Idea - I will make it selected in the first procedure and then do as you say.

Thanks for the guidance.