DataGridView 单元格、RowHeader 和 ColumnHeader 的不同 ContextMenuStrip

发布于 2024-11-25 03:08:18 字数 210 浏览 1 评论 0原文

我想为 DataGridView CellsRowHeadersColumnHeaders 设置不同的 ContextMenuStrip

这个想法是,当我右键单击这些项目中的任何一个时,都会显示不同的 ContextMenuStrip 。我该怎么做?

I want to set different ContextMenuStrip for DataGridView Cells, RowHeaders and ColumnHeaders.

The idea is that when I right-click any of these items, a different ContextMenuStrip is displayed. How do I do this?

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

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

发布评论

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

评论(2

如果没有 2024-12-02 03:08:18

使用 DataGridView 的 MouseDown 事件来测试是否单击了鼠标右键,如果是,则使用关联的 HitTestInfo 属性来确定是否单击了单元格、行或列。使用此信息来显示您需要的 ContextMenuStrip。

下面是执行此操作的 MouseDown 事件示例。要尝试该示例,请在表单上放置一个 DataGridView 和三个 ContentMenuStrip。将 ContentMenuStrip 命名为 mnuCell、mnuRow 和 mnuColumn。

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
        Dim ht As DataGridView.HitTestInfo
        ht = Me.DataGridView1.HitTest(e.X, e.Y)
        If ht.Type = DataGridViewHitTestType.Cell Then
            DataGridView1.ContextMenuStrip = mnuCell
            mnuCell.Items(0).Text = String.Format("This is the cell at {0}, {1}", ht.ColumnIndex, ht.RowIndex)
        ElseIf ht.Type = DataGridViewHitTestType.RowHeader Then
            DataGridView1.ContextMenuStrip = mnuRow
            mnuRow.Items(0).Text = "This is row " + ht.RowIndex.ToString()
        ElseIf ht.Type = DataGridViewHitTestType.ColumnHeader Then
            DataGridView1.ContextMenuStrip = mnuColumn 
            mnuColumn.Items(0).Text = "This is col " + ht.ColumnIndex.ToString()
        End If
    End If
End Sub

在这里,我将 DataGridView 的 ContextMenuStrip 属性分配给适合右键单击的项目(单元格、行或列)的 ContextMenuStrip。为了演示如何进一步自定义 ContextMenuStrips 的行为,我还在每个 ContentMenuStrips 的菜单项中设置文本。

Use the DataGridView's MouseDown event to test if the right mouse has been clicked and if so use the associated HitTestInfo property to determine if a cell, row or column has been clicked. Use this information to display the ContextMenuStrip you need.

Here's an example MouseDown event that does this. To try the sample drop a DataGridView and three ContentMenuStrips on a form. Name the ContentMenuStrips mnuCell, mnuRow and mnuColumn.

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
        Dim ht As DataGridView.HitTestInfo
        ht = Me.DataGridView1.HitTest(e.X, e.Y)
        If ht.Type = DataGridViewHitTestType.Cell Then
            DataGridView1.ContextMenuStrip = mnuCell
            mnuCell.Items(0).Text = String.Format("This is the cell at {0}, {1}", ht.ColumnIndex, ht.RowIndex)
        ElseIf ht.Type = DataGridViewHitTestType.RowHeader Then
            DataGridView1.ContextMenuStrip = mnuRow
            mnuRow.Items(0).Text = "This is row " + ht.RowIndex.ToString()
        ElseIf ht.Type = DataGridViewHitTestType.ColumnHeader Then
            DataGridView1.ContextMenuStrip = mnuColumn 
            mnuColumn.Items(0).Text = "This is col " + ht.ColumnIndex.ToString()
        End If
    End If
End Sub

Here I'm assigning the DataGridView's ContextMenuStrip property to the ContextMenuStrip appropriate for the item right clicked (cell, row or column). To demonstrate how you might further customize the behavior of the ContextMenuStrips I'm also setting the text in each ContentMenuStrips' menu item.

想你只要分分秒秒 2024-12-02 03:08:18

在 DataGridView 的 MouseDown 事件上,使用 DataGridView.HitTest 方法检查单击的内容。然后您可以根据单击的内容切换上下文菜单。

On MouseDown event of DataGridView, use DataGridView.HitTest method to check what was clicked. Then you can switch context menus depending on what was clicked.

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