Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 387
  • Last Modified:

contextmenus - accessing control they are associated with

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)
            {
                dgvReportAssignment.Rows.Remove(row);
            }

        }


Thanks for your help!
0
babybird
Asked:
babybird
1 Solution
 
Bob LearnedCommented:
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)
     {
        dgv.Rows.Remove(row);
     }

   }

Bob
0
 
babybirdAuthor Commented:
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!
0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now