Context Menu on Grid

Posted on 2006-04-26
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
    LVL 62

    Accepted Solution

    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

    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 62

    Expert Comment

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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
    Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    779 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

    15 Experts available now in Live!

    Get 1:1 Help Now