private void Grid_CellContextMenuStripNeeded(object sender, DataGridViewCellContextMenuStripNeededEventArgs e) { DataGridView grid = (sender as DataGridView); StripDataEx data = new StripDataEx { RowIndex = e.RowIndex, ColIndex = e.ColumnIndex, Grid = grid }; if (e.RowIndex != -1) { // if not header add the DataBoundItem try { data.DataBoundItem = grid.Rows[e.RowIndex].DataBoundItem; } catch (Exception) { } } if (e.ColumnIndex != -1) { data.HeaderText = grid.Columns[e.ColumnIndex].HeaderText; if (data.DataBoundItem != null) { data.Getter = data.DataBoundItem.GetType().GetProperty(grid.Columns[e.ColumnIndex].DataPropertyName); if (data.Getter != null) { data.ItemData = data.Getter.GetValue(data.DataBoundItem); } } } ContextStripEventArgs args = new ContextStripEventArgs { StripDataEx = data, Added = false }; StripDataNeeded(this, args); if (args.ToolStripDescriptions.Count > 0) { ContextMenuStrip strip = new ContextMenuStrip(); string grpName = args.ToolStripDescriptions[0].GroupName; foreach (ToolStripDescription item in args.ToolStripDescriptions) { if (grpName != item.GroupName) { strip.Items.Add(new ToolStripMenuItem("-")); grpName = item.GroupName; } strip.Items.Add(item.Item); } e.ContextMenuStrip = strip; } }
private void MenuHelper_StripDataNeeded(object sender, ContextStripEventArgs args) { List <ToolStripItem> lst = CreateToolStripItems(args.StripDataEx); if (lst != null) { foreach (ToolStripItem item in lst) { args.ToolStripDescriptions.Add(new ToolStripDescription { GroupName = "Filter", GroupOrder = 10, Item = item }); } } args.Added = true; }