I am trying to modify a DataGridView object through the use of a right-click menu.
At the moment I can successfully create the right-click menu and identify the method that will be called after the menu item was selected, however the target method can not access information about which record within the DataGridView was selected when the menu item was selected -- or any other information for that matter (unless it was a class-level variable).
My goal is to find a way to either send information to the target method, or find a way to modify the DataGridView object within the same method that created the right-click menu.
https://msdn.microsoft.com/en-us/library/system.windows.forms.menuitem(v=vs.110).aspx
private void InspectionDataGridViewCellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
ContextMenu m = new ContextMenu();
if (this.currentInspection != null) // the row that is being right-clicked might not be the row that is selected.
{
m.MenuItems.Add(new MenuItem(string.Format("Mark inspection point {0} as complete.", this.currentInspection.InspectionNumber), this.markInspectionPointComplete));
}
m.Show(this.InspectionDataGridView, new Point(e.X,e.Y));
}
}
private void markInspectionPointComplete(object sender, EventArgs e)
{
MessageBox.Show("the right-click menu works.");
}
I tried invoking the target method using a DataGridViewCellMouseEventArgs object, but that creates an error with the m.MenuItems.Add() line because it ONLY expects an EventArgs object.
So, either I need to modify the EventArgs object that is sent, find another method signature that will serve this purpose, or find a way to perform actions on the DataGridView within the same method that created the right-click menu.
Thanks in advance!
The simplest solution is to use 'Tag' property of the 'MenuItem' object.
private void InspectionDataGridViewCellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
ContextMenu m = new ContextMenu();
int currentRow = e.RowIndex;
if (this.currentInspection != null) // the row that is being right-clicked might not be the row that is selected.
{
var MI = new MenuItem(string.Format("Mark inspection point {0} as complete.", this.currentInspection.InspectionNumber), this.markInspectionPointComplete)
MI.Tag = e;
m.MenuItems.Add(MI);
}
m.Show(this.InspectionDataGridView, new Point(e.X,e.Y));
}
}
private void markInspectionPointComplete(object sender, EventArgs e)
{
var MI = (MenuItem)sender;
DataGridViewCellMouseEventArgs Obj = (DataGridViewCellMouseEventArgs) MI.Tag;
// Do whatever you want with your Obj
MessageBox.Show("the right-click menu works.");
}
OR
Use lambda expressions like this:
private void InspectionDataGridViewCellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
ContextMenu m = new ContextMenu();
int currentRow = e.RowIndex;
if (this.currentInspection != null) // the row that is being right-clicked might not be the row that is selected.
{
var MI = new MenuItem(string.Format("Mark inspection point {0} as complete.", this.currentInspection.InspectionNumber));
MI.Click += (s, x) =>
{
// Use 'e' or 'sender' here
}
m.MenuItems.Add(MI);
}
m.Show(this.InspectionDataGridView, new Point(e.X,e.Y));
}
}