【问题标题】:C# DataGridView Right Click to ContextMenu Click Retrieve Cell ValueC# DataGridView 右键单击​​ ContextMenu 单击检索单元格值
【发布时间】:2013-06-18 01:18:18
【问题描述】:

我有一个 DataGridView。右键单击 DataGridView 的第 4 列中的单元格时,我创建了一个 ContextMenuStrip。然而,我被卡住了,因为在左键单击 ContextMenuStrip 菜单项时,我希望从右键单击的单元格中提取数据。

我想要的单元格是 ContextMenuStrip 的左上角,这正是我右键单击并指向我要获取数据的单元格的位置。 The screen grab just doesn't show the mouse cursor.

这是我目前所拥有的:

GridView1.MouseDown += new MouseEventHandler(this.dataGridView_MouseDown);

private void dataGridView_MouseDown(object sender, MouseEventArgs e)
    {

        if (e.Button == MouseButtons.Right)
        {
            var ht = dataGridView1.HitTest(e.X, e.Y);

            //Checks for correct column index
            if (ht.ColumnIndex == 4 && ht.RowIndex != -1)
            {
                //Create the ContextStripMenu for Creating the PO Sub Form
                ContextMenuStrip Menu = new ContextMenuStrip();
                ToolStripMenuItem MenuOpenPO = new ToolStripMenuItem("Open PO");
                MenuOpenPO.MouseDown += new MouseEventHandler(MenuOpenPO_Click);
                Menu.Items.AddRange(new ToolStripItem[] { MenuOpenPO });

                //Assign created context menu strip to the DataGridView
                dataGridView1.ContextMenuStrip = Menu;
            }

            else
                dataGridView1.ContextMenuStrip = null;
        }
    }

I think this post may be what I am looking for

但是,如果我改变:private void dataGridView_MouseDown(object sender, MouseEventArgs e)

private void dataGridView_MouseDown(object sender, DataGridViewCellMouseEventArgs e)

我不确定如何更改 GridView1.MouseDown += new MouseEventHandler(this.dataGridView_MouseDown); 所以我没有收到错误消息。或者有更好的方法吗?

最终解决方案在 Gjeltema 的帮助下

dataGridView1.CellMouseDown += this.dataGridView1_CellMouseDown;

    private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
    {
        //Checks for correct column index
        if (e.Button == MouseButtons.Right && e.ColumnIndex == 4 && e.RowIndex != -1)
        {
            //Create the ContextStripMenu for Creating the PO Sub Form
            ContextMenuStrip Menu = new ContextMenuStrip();
            ToolStripMenuItem MenuOpenPO = new ToolStripMenuItem("Open PO");
            MenuOpenPO.MouseDown += new MouseEventHandler(MenuOpenPO_Click);
            Menu.Items.AddRange(new ToolStripItem[] { MenuOpenPO });

            //Assign created context menu strip to the DataGridView
            dataGridView1.ContextMenuStrip = Menu;
            CellValue = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
        }

        else
            dataGridView1.ContextMenuStrip = null;
    }

【问题讨论】:

    标签: c# datagridview contextmenustrip


    【解决方案1】:

    如果您要使用该帖子的解决方案,请注意他订阅的是CellMouseDown 事件,而不是MouseDown 事件。这有不同的签名。

    另外,从 .Net 2.0 开始,您不需要所有的委托包装语法,您只需 += 匹配事件委托签名的函数,如下所示:

    // Your updated MouseDown handler function with DataGridViewCellMouseEventArgs
    GridView1.CellMouseDown += this.dataGridView_MouseDown;
    

    这样你就不会收到错误信息了,可以做你在帖子中看到的。

    【讨论】:

    • 谢谢!我仍在尝试围绕事件和代表进行思考。您的解决方案对我有用,在更改几行后,我能够在单击上下文菜单后获取单元格值。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-02-09
    • 2013-05-18
    • 2011-06-16
    • 1970-01-01
    • 2011-07-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多