Question

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
Was it helpful?

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;
    }
}

OTHER TIPS

You need to use hitTest to get the columnIndex you are looking for something like:

    If e.Button = Windows.Forms.MouseButtons.Right Then
        Dim hit As DataGridView.HitTestInfo = _
                DataGridView1.HitTest(e.X, e.Y)
        MsgBox(hit.ColumnIndex.ToString)
    End If

I am not personally familiar with the UltraGrid but it should have the same, as most grid controls do. If not you can use the following:

http://devcenter.infragistics.com/Support/KnowledgeBaseArticle.aspx?ArticleID=1750

and adapt it to give you the column instead.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top