Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. use same ContextMenu for different datagridviews

use same ContextMenu for different datagridviews

Scheduled Pinned Locked Moved C#
question
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    LordZoster
    wrote on last edited by
    #1

    Hallo I put several dgv's in a form, and I created a ContextMenuStrip to perform same common operations on dgv's "CurrentRow" record (eg. Deletion). Is there any way I could reuse the same ContextMenuStrip without duplicating, maybe passing the dgv object as an argument? I would avoid to rewrite a control that inherits and extends ContextMenuStrip. Thanks in advance

    L 1 Reply Last reply
    0
    • L LordZoster

      Hallo I put several dgv's in a form, and I created a ContextMenuStrip to perform same common operations on dgv's "CurrentRow" record (eg. Deletion). Is there any way I could reuse the same ContextMenuStrip without duplicating, maybe passing the dgv object as an argument? I would avoid to rewrite a control that inherits and extends ContextMenuStrip. Thanks in advance

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      LordZoster wrote:

      maybe passing the dgv object as an argument?

      It's passed by default, in the sender-parameter. That's of type object, so you'd have to cast it to a DataGridView.

      I are Troll :suss:

      L 1 Reply Last reply
      0
      • L Lost User

        LordZoster wrote:

        maybe passing the dgv object as an argument?

        It's passed by default, in the sender-parameter. That's of type object, so you'd have to cast it to a DataGridView.

        I are Troll :suss:

        L Offline
        L Offline
        LordZoster
        wrote on last edited by
        #3

        Thanks for replying, actually I get an exception of "casting a ToolStripItem type on DataGridView type".

        private void deleteToolStripMenuItem\_Click(object sender, EventArgs e)
        {
          DataGridView dgv = (DataGridView)sender;
          if (dgv.CurrentRow.Index > -1)
            dgv.Rows.Remove(dgv.CurrentRow);
        }
        

        modified on Friday, March 18, 2011 6:08 AM

        L L 2 Replies Last reply
        0
        • L LordZoster

          Thanks for replying, actually I get an exception of "casting a ToolStripItem type on DataGridView type".

          private void deleteToolStripMenuItem\_Click(object sender, EventArgs e)
          {
            DataGridView dgv = (DataGridView)sender;
            if (dgv.CurrentRow.Index > -1)
              dgv.Rows.Remove(dgv.CurrentRow);
          }
          

          modified on Friday, March 18, 2011 6:08 AM

          L Offline
          L Offline
          LordZoster
          wrote on last edited by
          #4

          Solved.

          private void eliminaToolStripMenuItem\_Click(object sender, EventArgs e)
          {
            ToolStripDropDownItem menu = (ToolStripDropDownItem)sender;
            ContextMenuStrip strip = (ContextMenuStrip)menu.Owner;
            Control owner = strip.SourceControl;
            DataGridView dgv = (DataGridView)owner;
              if (dgv.CurrentRow.Index > -1)
                dgv.Rows.Remove(dgv.CurrentRow);
          }
          
          1 Reply Last reply
          0
          • L LordZoster

            Thanks for replying, actually I get an exception of "casting a ToolStripItem type on DataGridView type".

            private void deleteToolStripMenuItem\_Click(object sender, EventArgs e)
            {
              DataGridView dgv = (DataGridView)sender;
              if (dgv.CurrentRow.Index > -1)
                dgv.Rows.Remove(dgv.CurrentRow);
            }
            

            modified on Friday, March 18, 2011 6:08 AM

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            My apologies; it's the ToolStripItem that initiates the action, and that'll be the sender indeed. Thanks for posting the corrected version, always usefull as a future reference :)

            I are Troll :suss:

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups