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