Question

Right clicking on the empty part of a Ultragrid (Infragistics) or on the header in C# context menu appears and dont do anything. How can I only have the context menu appear when the click falls over a row?

SO I am working on a project where I have a ultra grid and I put a context menu in it which is when someone's right click in the grid the menu appears (delete). But the Context menu appears on the blank space as well as on the Ultra grid header when right click and i want it appear when the click falls over a row.

Was it helpful?

Solution

This need to be tested in your environment, but I think it could work.
The trick consist in using the MouseDown event to check the cell under the mouse position (if any) and assign the ContextMenu only if we are over a DataRow cell testing the IsDataRow property.

private void grid_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
    UltraGridCell currentCell = null;
    grid.ContextMenu = null;

    if (e.Button == MouseButtons.Right)
    {
        Point ulPoint = new Point(e.X, e.Y);
        UIElement el = grid.DisplayLayout.UIElement.ElementFromPoint(ulPoint); 
        if (el != null)
            currentcell = (UltraGridCell)el.GetContext(typeof(UltraGridCell)); 
        if (currentcell != null && currentCell.Row.IsDataRow == true)
        {
            grid.ActiveCell = currentcell;
            grid.ContextMenu = menuPopup;
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top