Hi,
How to display a context menu in an ultra grid if and only if the user clicks on a particular row. And the row or cell should not be header or blank cell/row.
Regards,
SreeRam
Hi SreeRam,
You can't apply a context menu to a part of a control. What you would have to do is apply the context menu to the entire control depending on the location of the mouse. I would use the MouseEnterElement and MouseLeaveElement events of the grid to assign and remove the context menu. You can use the GetContext method on the UIElement being entered to get the row (or null) and either set or remove the context menu on the grid control.
Hi Mike,
As you said we can't apply a context menu to a part of a control if we choose the "ContextMenuStrip" property for ultragrid control. I tried MouseDown event to assign or remove the context menu. Although the below code works fine if and only if you remove the "ContextMenuStrip" property for ultragrid control. Because it overrides the below code and displays the context menu whereever you click the mouse within the control.
********************************************************************************
private void ugQueue_MouseDown(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Right) { Point mousePoint = new Point(e.X, e.Y); UIElement element = ((UltraGrid)sender).DisplayLayout.UIElement.ElementFromPoint(mousePoint); UltraGridCell cell = element.GetContext(typeof(UltraGridCell)) as UltraGridCell; if (cell != null) { if (Convert.ToByte(cell.Row.Cells["Status"].Value) == 0) { cmnuQueue.Show(ugQueue, mousePoint); cell.Row.Selected = true; } else return; } } }
Thanks Mike.
With Regards,
SreeRam.
That's another good way to do it. I would recommend using MouseUp instead of MouseDown, though. If you try this with any Windows application, you can see that context menus are always display in the MouseUp. Displaying a context menu in MouseDown can cause problems because the control gets a MouseDown, but the MouseUp message is lost.
I would like to know if I right-clicked on the cell header. How do I do this?
My current code:
Private Sub ugCommission_MouseUp(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles ugCommission.MouseUp Dim cmItemNum As New ContextMenuStrip Dim cmCommDue As New ContextMenuStrip If e.Button = Windows.Forms.MouseButtons.Right Then Dim ptCurrent As Point = New Point(e.X, e.Y) Dim uiElement As Infragistics.Win.UIElement = CType(sender, UltraGrid).DisplayLayout.UIElement.ElementFromPoint(ptCurrent) Dim ugCell As UltraGridCell = uiElement.GetContext(GetType(UltraGridCell)) Dim ugRow As UltraGridRow = uiElement.GetContext(GetType(UltraGridRow)) ' Thought ugUIElement would be it, but it is also nothing when I right click on the col header cell Dim ugUIElement As UltraGridUIElement = uiElement.GetContext(GetType(UltraGridUIElement)) If ugCell IsNot Nothing Then ugCommission.Selected.Rows.Clear() ugRow.Selected = True If ugCell.Column.Key = "ITEM_NUM" Then _cmItemNum.Show(ugCommission, ptCurrent) ElseIf ugCell.Column.Key = "COMM_DUE" Then _cmCommDue.Show(ugCommission, ptCurrent) End If End If End If End Sub
As mike said you can also try with ColumnHeader. But the coding pattern remains same.