右键单击 datagridview 的上下文菜单 [英] right click context menu for datagridview

查看:35
本文介绍了右键单击 datagridview 的上下文菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 .NET winform 应用程序中有一个 datagridview.我想右键单击一行并弹出一个菜单.然后我想选择诸如复制、验证等内容

I have a datagridview in a .NET winform app. I would like to rightclick on a row and have a menu pop up. Then i would like to select things such as copy, validate, etc

我如何制作 A) 弹出菜单 B) 找到右键单击的行.我知道我可以使用 selectedIndex 但我应该能够在不更改所选内容的情况下右键单击?现在我可以使用选定的索引,但如果有一种方法可以在不更改所选内容的情况下获取数据,那么这将很有用.

How do i make A) a menu pop up B) find which row was right clicked. I know i could use selectedIndex but i should be able to right click without changing what is selected? right now i could use selected index but if there is a way to get the data without changing what is selected then that would be useful.

推荐答案

您可以使用 CellMouseEnter 和 CellMouseLeave 来跟踪鼠标当前悬停的行号.

You can use the CellMouseEnter and CellMouseLeave to track the row number that the mouse is currently hovering over.

然后使用 ContextMenu 对象来显示为当前行定制的弹出菜单.

Then use a ContextMenu object to display you popup menu, customised for the current row.

这是我的意思的一个快速而肮脏的例子......

Here's a quick and dirty example of what I mean...

private void dataGridView1_MouseClick(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Right)
    {
        ContextMenu m = new ContextMenu();
        m.MenuItems.Add(new MenuItem("Cut"));
        m.MenuItems.Add(new MenuItem("Copy"));
        m.MenuItems.Add(new MenuItem("Paste"));

        int currentMouseOverRow = dataGridView1.HitTest(e.X,e.Y).RowIndex;

        if (currentMouseOverRow >= 0)
        {
            m.MenuItems.Add(new MenuItem(string.Format("Do something to row {0}", currentMouseOverRow.ToString())));
        }

        m.Show(dataGridView1, new Point(e.X, e.Y));

    }
}

这篇关于右键单击 datagridview 的上下文菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆