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 ToolStripMenuItem CreateMenuitem(string txt, GridFilter <T> gf, StripDataEx dataEx, bool clickable = false) { ToolStripMenuItem item = new ToolStripMenuItem(txt); item.Tag = new FilterStatus { GridFilter = gf, StripDataEx = dataEx }; if (clickable) { item.Click += Item_Click; } return(item); }
private List <ToolStripItem> CreateToolStripItems(StripDataEx stripDataEx) { List <ToolStripItem> tsList = new List <ToolStripItem>(); if (currentFilter != null) { ToolStripMenuItem item = CreateMenuitem("Clear Filter", null, stripDataEx, true); tsList.Add(item); item = CreateMenuitem(currentFilter); tsList.Add(item); } if (stripDataEx.Getter != null) { List <GridFilter <T> > filterDescLst = new List <GridFilter <T> >(); if (Filters.TryGetValue(stripDataEx.Getter, out List <GridFilter <T> > result)) { filterDescLst.AddRange(result); } if (filterDescLst.Count == 0) { filterDescLst.Add(EqualFilter()); } foreach (var filterdesc in filterDescLst) { FilterStatus status = new FilterStatus { GridFilter = filterdesc, StripDataEx = stripDataEx, IsActive = false }; if (currentFilter == null || (currentFilter.GridFilter != status.GridFilter || currentFilter.StripDataEx.Getter != status.StripDataEx.Getter || currentFilter.StripDataEx.ItemData == status.StripDataEx.ItemData)) { tsList.Add(CreateMenuitem(status)); } } } return(tsList); }