Solved

ContextMenuStrip with DataGridView problem

Posted on 2008-10-05
10
506 Views
Last Modified: 2013-11-07
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!
0
Comment
Question by:RossEdwards
  • 5
  • 5
10 Comments
 
LVL 48

Expert Comment

by:jpaulino
ID: 22646068
>> 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
0
 
LVL 1

Author Comment

by:RossEdwards
ID: 22646135
How?
0
 
LVL 48

Expert Comment

by:jpaulino
ID: 22646351
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

0
 
LVL 1

Author Comment

by:RossEdwards
ID: 22646360
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!
0
 
LVL 48

Expert Comment

by:jpaulino
ID: 22646368
Can you show the code you have ?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 1

Author Comment

by:RossEdwards
ID: 22646381
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

0
 
LVL 48

Expert Comment

by:jpaulino
ID: 22646390
But you have the information on a variable ?
Private Sub flagselected() Handles FlagToolStripMenuItem.Click
       MessageBox.Show(orderid)
End Sub
0
 
LVL 1

Author Comment

by:RossEdwards
ID: 22646393
I know - orderid is a global variable.  I would rather not use it.  Hence my question - is there a better way of doing it?  
0
 
LVL 48

Accepted Solution

by:
jpaulino earned 500 total points
ID: 22646403
>> orderid is a global variable
You are defining as public or as private ?
When you right click on a datagridview, the cell can be selected or not, and this way this is the better way I know. If you have the row selected you could use me.DataGridView1.CurrentRow or other property.
0
 
LVL 1

Author Closing Comment

by:RossEdwards
ID: 31503240
Good Idea - I will make it selected in the first procedure and then do as you say.

Thanks for the guidance.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
VB.Net - Oracle BulkCopy from CSV Date Format 7 47
Vb.Net Date Formatting Assistance 4 36
Put window Form inside tab page 10 26
Variable Event ? 3 23
Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now