Search code examples
vb.netinfragisticsultrawingrid

How to get column index of ultra grid view on right click


When i right click on ultra grid view or may be grid view . I want to display different contextmenu strip for different columns. But when i right click i get index of column which i selected not which i right clicked. How should i get it.. Code is as follows:

 Private Sub DataGridView1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DataGridView1.MouseDown
       If e.Button() = Windows.Forms.MouseButtons.Right Then
            MessageBox.Show(DataGridView1.SelectedCells(0).ColumnIndex.ToString())
        End If
    End Sub

Solution

  • The best way to get UltraColumn under the cursor is to utilize MouseUp event of the UltraGrid class. Here is sample in C# but i'm sure you will catch my idea:

    private void Grid_MouseUp(object sender, MouseEventArgs e)
    {
        UltraGrid Grid = sender as UltraGrid;
        if (Grid.DisplayLayout == null)
            return;
        UIElement ue = Grid.DisplayLayout.UIElement.ElementFromPoint(new Point(e.X, e.Y));
        if (ue == null)
            return;
    
        UIElement mouseupItem = ue;
        UltraGridColumn mouseupColumn = null;
        UltraGridRow mouseupRow = null;
        UltraGridBand mouseupBand = null;
        ColumnHeader mouseupColumnHead = null;
    
        mouseupColumn = (UltraGridColumn)ue.GetContext(typeof(UltraGridColumn), true);
        mouseupRow = (UltraGridRow)ue.GetContext(typeof(UltraGridRow), true);
        mouseupBand = (UltraGridBand)ue.GetContext(typeof(UltraGridBand), true);
        mouseupColumnHead = (ColumnHeader)ue.GetContext(typeof(ColumnHeader), true);
    
        if (mouseupColumnHead != null)
            mouseupColumn = mouseupColumnHead.Column;
    
        if (e.Button == MouseButtons.Right)
        {
            ShowPopupMenu( mouseupColumn );
            return;
        }
    }