I've an UltraGrid like this:
Column1 Column2 Column3 ---------- 0 ABC ABB 0 XYZ XXZ 1 CCB AAA 2 QWE ASD 1 ASX ZAW
I'm trying to add context menu to a specific row/column in UltraWinGrid, i.e, if column1 = 0 then contextmenu1, if coumn1 =1 then contextmenu2, if column1 = 2 then contextmenu3 ( all the 3 contextmenus are have different menu items.) Can you please help???
Use the MouseDown event of the UltraGrid. Then you can do something such as:
ContextMenu defaultContextMenu; public frmMain() { InitializeComponent(); defaultContextMenu = ultraGrid1.ContextMenu; } private void ultraGrid1_MouseDown(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Right) { UIElement element = ultraGrid1.DisplayLayout.UIElement.ElementFromPoint(e.Location); if (element == null || element.Parent == null) return; Infragistics.Shared.ISelectableItem theitem = element.SelectableItem; if (theitem is UltraGridCell) { ultraGrid1.ActiveRow = ((UltraGridCell)theitem).Row; ultraGrid1.ActiveCell = theitem as UltraGridCell; switch (ultraGrid1.ActiveCell.Column.Key) { case "Column1": ultraGrid1.ContextMenu = contextMenu1; break; case "Column2": ultraGrid1.ContextMenu = contextMenu2; break; case "Column3": ultraGrid1.ContextMenu = contextMenu3; break; default: ultraGrid1.ContextMenu = defaultContextMenu; break; } } else { ultraGrid1.ContextMenu = defaultContextMenu; } } }
It is better to use the location of the mouse click instead of the active cell. Use the following to get the column name based on where the mouse was clicked (this is VB.Net):
Dim cell As UltraGridCell = CType(element.GetContext(GetType(UltraGridCell)), UltraGridCell)
Then use this to get the key name for the cell:
cell.Column.Key