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. GridView: row focus on mouse right click before ContextMenu pops up.

GridView: row focus on mouse right click before ContextMenu pops up.

Scheduled Pinned Locked Moved C#
cssquestion
6 Posts 4 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.
  • R Offline
    R Offline
    Raybarg
    wrote on last edited by
    #1

    Google and I were not friends today, so I ask you guys here! This project has GridView control and MenuStrip which is set in grid's ContextMenuStrip to pop up when user is pressing right mouse button over the grid. When user is pressing right mouse button over GridView the current row is not automatically set to the row "under" mouse pointer. How is it possible to change current row (focus on) to the row under mouse pointer at that point before toolstrip menu is shown?

    L E M 3 Replies Last reply
    0
    • R Raybarg

      Google and I were not friends today, so I ask you guys here! This project has GridView control and MenuStrip which is set in grid's ContextMenuStrip to pop up when user is pressing right mouse button over the grid. When user is pressing right mouse button over GridView the current row is not automatically set to the row "under" mouse pointer. How is it possible to change current row (focus on) to the row under mouse pointer at that point before toolstrip menu is shown?

      L Offline
      L Offline
      led mike
      wrote on last edited by
      #2

      Raybarg wrote:

      How is it possible to change current row (focus on) to the row under mouse pointer at that point before toolstrip menu is shown?

      It may not be. Without looking at the Grid documentation I would guess, like other controls, you can determine the row based on the mouse pointer coordinates. Keep in mind this is a complete guess on my part and if I am wrong someone else will likely reply to you with the correct solution.

      1 Reply Last reply
      0
      • R Raybarg

        Google and I were not friends today, so I ask you guys here! This project has GridView control and MenuStrip which is set in grid's ContextMenuStrip to pop up when user is pressing right mouse button over the grid. When user is pressing right mouse button over GridView the current row is not automatically set to the row "under" mouse pointer. How is it possible to change current row (focus on) to the row under mouse pointer at that point before toolstrip menu is shown?

        E Offline
        E Offline
        Ennis Ray Lynch Jr
        wrote on last edited by
        #3

        In the cell mouse down event open your context menu rather than using the built in context menu property.

        Need custom software developed? I do C# development and consulting all over the United States. A man said to the universe: "Sir I exist!" "However," replied the universe, "The fact has not created in me A sense of obligation." --Stephen Crane

        1 Reply Last reply
        0
        • R Raybarg

          Google and I were not friends today, so I ask you guys here! This project has GridView control and MenuStrip which is set in grid's ContextMenuStrip to pop up when user is pressing right mouse button over the grid. When user is pressing right mouse button over GridView the current row is not automatically set to the row "under" mouse pointer. How is it possible to change current row (focus on) to the row under mouse pointer at that point before toolstrip menu is shown?

          M Offline
          M Offline
          Manas Bhardwaj
          wrote on last edited by
          #4

          The row is selected on the left mouse down. while in your case, you do a right mouse click to show the menu strip. You can instead fake this in the OnCellMouseClickEvent

          if (e.Button == MouseButtons.Right)
          {
          if(this.datagridview1.SelectedRows.Count > 0)
          this.datagridview1.SelectedRows[0].Selected = false;
          this.datagridview1.Rows[e.RowIndex].Selected = true;
          }

          Manas Bhardwaj Please remember to rate helpful or unhelpful answers, it lets us and people reading the forums know if our answers are any good.

          R 2 Replies Last reply
          0
          • M Manas Bhardwaj

            The row is selected on the left mouse down. while in your case, you do a right mouse click to show the menu strip. You can instead fake this in the OnCellMouseClickEvent

            if (e.Button == MouseButtons.Right)
            {
            if(this.datagridview1.SelectedRows.Count > 0)
            this.datagridview1.SelectedRows[0].Selected = false;
            this.datagridview1.Rows[e.RowIndex].Selected = true;
            }

            Manas Bhardwaj Please remember to rate helpful or unhelpful answers, it lets us and people reading the forums know if our answers are any good.

            R Offline
            R Offline
            Raybarg
            wrote on last edited by
            #5

            Yes, that works with the solution Ennis suggested to pop the menu after doing that. And here is how I got the menu positioned correctly:

                    Point pt = MyGrid.PointToClient(Control.MousePosition);
                    MyMenuStrip.Show(MyGrid, pt.X, pt.Y);
            

            Thank you all for helping in this simple matter.

            1 Reply Last reply
            0
            • M Manas Bhardwaj

              The row is selected on the left mouse down. while in your case, you do a right mouse click to show the menu strip. You can instead fake this in the OnCellMouseClickEvent

              if (e.Button == MouseButtons.Right)
              {
              if(this.datagridview1.SelectedRows.Count > 0)
              this.datagridview1.SelectedRows[0].Selected = false;
              this.datagridview1.Rows[e.RowIndex].Selected = true;
              }

              Manas Bhardwaj Please remember to rate helpful or unhelpful answers, it lets us and people reading the forums know if our answers are any good.

              R Offline
              R Offline
              Raybarg
              wrote on last edited by
              #6

              Hi again! Since I had CurrencyManager in BindingContext, your suggested solution resulted in CurrencyManager position not changing when user right-clicked the grid so I had to google again, this time with more words in the issue to succeed. Here's the solution: Using DataGridViewCellMouseEventArgs e I can change current row by setting CurrentCell property:

              this.datagridview1.CurrentCell = this.datagridview1[e.ColumnIndex, e.RowIndex];

              And I repaste the code submitted in other post which pops the context menu also:

              Point pt = datagridview1.PointToClient(Control.MousePosition);
              menustrip1.Show(datagridview1, pt.X, pt.Y);

              Thanks for your help again, the grid is working exactly like I want it to now!

              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