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
As mike said you can also try with ColumnHeader. But the coding pattern remains same.
I assume by "cell header" that you mean the column header.
You can do this using basically the same code you have here. You just check for a ColumnHeader type when you call GetContext.
By the way, you can simplify your code here a little bit by using the LastElementEntered property instead of messing around with points.
Private Sub UltraGrid1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles UltraGrid1.MouseDown Dim grid As UltraGrid = CType(sender, UltraGrid) If e.Button = Windows.Forms.MouseButtons.Right Then Dim element As UIElement = grid.DisplayLayout.UIElement.LastElementEntered Dim ugCell As UltraGridCell = element.GetContext(GetType(UltraGridCell)) Dim ugRow As UltraGridRow = element.GetContext(GetType(UltraGridRow)) Dim columnHeader As ColumnHeader = element.GetContext(GetType(ColumnHeader)) If columnHeader IsNot Nothing Then MessageBox.Show(columnHeader.Caption) End If End If End Sub
As I understood that you want to know whether the user clicked on the column header or not? If this is correct, You can use the below code to detect whether the user clicks on the column header or not. If I misunderstood your scenario please elaborate your question so that I can help.
Dim ugColl As UltraGridColumn = uiElement.GetContext(GetType(UltraGridColumn))
If (ugCell Is Nothing And ugColl IsNot Nothing) Then ' your code goes here
End If
dpbouchard said: 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
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
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.