【问题标题】:How to get column index of ultra grid view on right click如何在右键单击时获取超网格视图的列索引
【发布时间】:2012-01-08 12:27:05
【问题描述】:

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

 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

【问题讨论】:

    标签: vb.net infragistics ultrawingrid


    【解决方案1】:

    您需要使用 hitTest 来获取您正在寻找的 columnIndex,例如:

        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

    并对其进行调整以提供给您的列。

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-08-12
        • 2018-07-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多