如何在右键单击中获取超网格视图的列索引

时间:2011-11-29 05:06:02

标签: vb.net infragistics ultragrid ultrawingrid

当我右键单击超网格视图或可能是网格视图时。 我想为不同的列显示不同的contextmenu strip。 但是当我右键单击时,我得到了我选择的列的索引,而不是我右键单击的列。 我应该怎么做到.. 代码如下:

 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

2 个答案:

答案 0 :(得分:3)

在光标下获取UltraColumn的最佳方法是利用 UltraGrid 类的 MouseUp 事件。 这是C#中的示例,但我相信你会理解我的想法:

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

答案 1 :(得分:1)

您需要使用hitTest来获取您正在寻找类似的列的索引:

    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

我并不熟悉UltraGrid,但它应该与大多数网格控件一样。如果没有,您可以使用以下内容:

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

并对其进行修改以改为为您提供专栏。

相关问题