contextmenus - accessing control they are associated with

Posted on 2006-05-31
Last Modified: 2008-02-20
The following method is from a ContextStripMenu Item.  I want the method to delete all the selected rows of a datagridview, which it does.  My issue is I have 4 datagridviews that use the same ContextStripMenu and I would like to pass in the datagridview name dynamically.  Is their a way to pass the datagridview through e or find it in the sender object?  Or is their another way to do this, without creating 4 separate ContextStripMenus for each datagridview.

        private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
            foreach (DataGridViewRow row in this.dgvReportAssignment.SelectedRows)


Thanks for your help!
Question by:babybird
    LVL 96

    Accepted Solution

    Try this:

      private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
         ContextMenuStrip strip = ((ToolStripMenuItem) sender).Owner;
         DataGridView dgv = (DataGridView) strip.SourceControl;

         foreach (DataGridViewRow row in dgv.SelectedRows)



    Author Comment

    this worked great! Thank you.

    We did make a minor change to the strip object instantiation due to a conversion error and replaced it with the following:

                ToolStripMenuItem ts = (ToolStripMenuItem)sender;
                ContextMenuStrip strip = (ContextMenuStrip)ts.Owner;

    It worked perfectly....I appreciate your help!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Article by: Najam
    Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
    Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    760 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

    7 Experts available now in Live!

    Get 1:1 Help Now