[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


Context Menu on Grid

Posted on 2006-04-26
Medium Priority
Last Modified: 2010-04-23
I have looked over this code, but it does not work for me.


The error is on this line:

Dim objGrid As DataGrid = CType(sender, DataGrid)

The error received states:

"Specifid Cast not valid."

I need to right click on a grid (I have several grids on a page) and get the grid name or selected grid.

Question by:Gary2397
  • 2
LVL 64

Accepted Solution

Fernando Soto earned 2000 total points
ID: 16550101
Hi Gary2397;

Well the other code is wrong. When you click on a context menu and go into its click event as below the object sender is not the DataGrid but always the MenuItem object that was selected. So to get the control that the menu item was click on you must use the context menu object itself. In the code sample below replace the ContextMenu1 with the name of your ContextMenu.

    Private Sub MenuItem1_Click(ByVal sender As Object, _
        ByVal e As System.EventArgs) Handles MenuItem1.Click

        Select Case ContextMenu1.SourceControl.Name
            Case "DataGrid1"
                MessageBox.Show("DataGrid 1 Was Right Clicked On")
            Case "DataGrid2"
                MessageBox.Show("DataGrid 2 Was Right Clicked On")
            Case "DataGrid3"
                MessageBox.Show("DataGrid 3 Was Right Clicked On")
            Case "DataGrid4"
                MessageBox.Show("DataGrid 4 Was Right Clicked On")
        End Select

    End Sub


Author Comment

ID: 16552977
Thank you. The code works perfect. The original code will be misleading to other people who view it, because it doesn't work. But someone accepted it as a correct answer. Thanks.
LVL 64

Expert Comment

by:Fernando Soto
ID: 16553027
Glad I was able to help. Have a great day. :=)

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

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

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

834 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