I want to set different ContextMenuStrip
for DataGridView
Cells
, RowHeaders
and ColumnHeaders
.
The idea is that when I right-click any of these items, a different ContextMenuStrip
is displayed. How do I do this?
contextmenustripdatagridviewheader-rowvb.netwinforms
I want to set different ContextMenuStrip
for DataGridView
Cells
, RowHeaders
and ColumnHeaders
.
The idea is that when I right-click any of these items, a different ContextMenuStrip
is displayed. How do I do this?
Best Answer
Use the DataGridView's
MouseDown
event to test if the right mouse has been clicked and if so use the associatedHitTestInfo
property to determine if a cell, row or column has been clicked. Use this information to display the ContextMenuStrip you need.Here's an example
MouseDown
event that does this. To try the sample drop a DataGridView and three ContentMenuStrips on a form. Name the ContentMenuStrips mnuCell, mnuRow and mnuColumn.Here I'm assigning the DataGridView's ContextMenuStrip property to the ContextMenuStrip appropriate for the item right clicked (cell, row or column). To demonstrate how you might further customize the behavior of the ContextMenuStrips I'm also setting the text in each ContentMenuStrips' menu item.