Context Menu on Grid

I have looked over this code, but it does not work for me.

http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/VB_DOT_NET/Q_21009380.html?query=context+menu&topics=866

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.

Gary2397Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Fernando SotoRetiredCommented:
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


Fernando
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Gary2397Author Commented:
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.
0
Fernando SotoRetiredCommented:
Glad I was able to help. Have a great day. :=)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.