如何通过右键单击获取超级网格视图的列索引

发布于 12-18 07:16 字数 448 浏览 5 评论 0原文

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

 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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

舟遥客2024-12-25 07:16:13

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

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;
    }
}
墨洒年华2024-12-25 07:16:13

您需要使用 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

并对其进行调整以提供该列。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文