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

查看:223
本文介绍了右键单击上下文菜单的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天全站免登陆