I have a datagrid where I would like to be able to right click, make it show a popup and capture the row it has been right clicked on. I have found something very similar here: right click context menu for datagridview but they all use the DataGridViewCellMouseEventArgs
because they are old. I found a relatively new one which is like this:
if (e.ChangedButton == MouseButton.Right)
{
System.Windows.Forms.ContextMenu m = new System.Windows.Forms.ContextMenu();
m.MenuItems.Add(new System.Windows.Forms.MenuItem("Cut"));
m.MenuItems.Add(new System.Windows.Forms.MenuItem("Copy"));
m.MenuItems.Add(new System.Windows.Forms.MenuItem("Paste"));
int currentMouseOverRow = datagrid.HitTest(e.X, e.Y).RowIndex;
if (currentMouseOverRow >= 0)
{
m.MenuItems.Add(new System.Windows.Forms.MenuItem(string.Format("Do something to row {0}", currentMouseOverRow.ToString())));
}
m.Show(datagrid, new Point(e.X, e.Y));
}
But I am getting errors on the m.Show(datagrid, new Point(e.X, e.Y));
and HitTest
. Any help would be appreciated.