Hi All
How are you doing.. I hope everybody is doing great. Here I have a requirement that I need to add context menu to the UltraWinGrid.. Can anybody help me with that.Like how to add a context menu to the UltraWinGrid and items to that menu and How to write click events for them.
Basically I need to add two items to the context menu for the grid rows and then if the user clicks on them I need to do some functionality.
If anybody could help in gudiing me through this.. it would be really helpful.
you add a context menu strip to your design and you set the property:
ultragrid.contextmenustrip to this added contextmenustrip
The grid is no different than any other control in this regard, really. You can use a ContextMenuStrip or you could use an UltraToolbarsManager to provide context menus to controls.
The only tricky part of this with the grid is that you might want to only show the menu when the user right-clicks on a cell or a row or some other part of the grid. For something like that, you would trap the grid's MouseMove event and set the ContextMenu based on the current location of the mouse.
To determine that the mouse is over, you use the UIElements of the grid:
HOWTO:UltraWinGrid Mouse Position and Cell Identification
HOWTO:UltraWinGrid Mouse Position and Column Identification
HOWTO:UltraWinGrid Mouse Position and Row Identification
This is one reason why MouseMove might be a better event to use than MouseDown. Or MouseEnterElement is another option.
But in fact, there is a way to trap MouseDown on a cell in edit mode.
FAQ:Mouse events such as MouseDown, MouseUp, and DoubleClick do not fire when the mouse is over a cell that is in edit mode.
This is true. That's what I do now - implement custom logic on MouseDown and MouseUp. It's a lot of code and it still does not resolve the issue of the right-click on an activated cell. Grid's MouseDown is not raised on successive right-clicks inside an active cell. Grid's Context Menu still appears (apparently it's passed to the cell's Editor), but the cell's Editor does not expose its own MouseDown event, so there is no way for me to trap this action and modify the Context Menu.
What you describe here is not limited to the grid. It's true of any complex control, include the DataGridView, TreeView, ListView and any control that has lists of items or multiple parts.
The typical thing to do in a case like this is to use the MouseMove or MouseDown event to change the ContextMenu on the control based on the current location of the mouse. Since ContextMenus do not display until MouseUp, I like to use MouseDown so I can trap the location of the mouse and the mouse button that was pressed.
So the only part of this that might be a bit tricky is determining what part of the grid the mouse is currently over. And the solution to that is quite simple:
Mike Saltzman"] How so? The grid's ContextMenu property is useful in all sorts of circumstances.
How so? The grid's ContextMenu property is useful in all sorts of circumstances.
ContextMenu usually makes sense in the context of a Grid's row or individual cell. Users want to Cut/Copy/Paste data from a cell or take some actions on the object represented by a record in the Grid. If I just assign a ContextMenuStrip object to the grid's ContextMenuStrip property, the Context Menu appears on any right click of the mouse within the visual scope of the grid, including headers, column grouping area, summary area, empty space, even scroll bars. Active/Selected row/cell/column is often undefined or is not clearly visible in such cases. Let alone the fact that I cannot implement more complex logic and swap/modify the Context Menu based on the actual context of a row/cell without taking over the control of the mouse right-click. It would be very helpful to (at very least) have an event BeforeContextMenuAppears.
Besides, when a cell is activated the Grid's MouseClick event no longer fires on mouse right-click(because I'm inside the editor control), so there is no way to enable/disable Cut/Copy/Paste items based on user's text selection/deselection inside the cell. And so on...
dm_nazaryev said:Well, that sucks
Nothing we can do about it. Blame it on the TextBox. :)
dm_nazaryev said:Especially taking into consideration that the grid’s ContextMenuStrip is essentially useless for all practical purposes.