简体   繁体   English

C#DataGridView右键单击ContextMenu单击“检索单元格值”

[英]C# DataGridView Right Click to ContextMenu Click Retrieve Cell Value

I have a DataGridView. 我有一个DataGridView。 I have created a ContextMenuStrip when Right Clicking a Cell in column 4 of my DataGridView. 在右键单击DataGridView的第4列中的单元格时,我创建了一个ContextMenuStrip。 I am however stuck because on left clicking the ContextMenuStrip Menu Item I would like the extract the data from the cell that was right clicked. 但是我被卡住了,因为在左键单击ContextMenuStrip菜单项我希望从右键单击的单元格中提取数据。

The cell I want is the top left corner of the ContextMenuStrip which is precisely where I right clicked and points at the cell who's data I want to grab. 我想要的单元格是ContextMenuStrip的左上角,这正是我右键单击的位置,并指向我要抓取的数据的单元格。 The screen grab just doesn't show the mouse cursor. 屏幕抓取只是不显示鼠标光标。

This is what I have so far: 这是我到目前为止:

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 我认为这篇文章可能就是我想要的

However if I change: private void dataGridView_MouseDown(object sender, MouseEventArgs e) to 但是,如果我更改: private void dataGridView_MouseDown(object sender, MouseEventArgs e) to

private void dataGridView_MouseDown(object sender, DataGridViewCellMouseEventArgs e)

I am not sure how to change GridView1.MouseDown += new MouseEventHandler(this.dataGridView_MouseDown); 我不知道如何更改GridView1.MouseDown += new MouseEventHandler(this.dataGridView_MouseDown); So I don't get an error message. 所以我没有收到错误消息。 Or is there a better way to do this? 或者有更好的方法吗?

Final Solution With help from Gjeltema 最终解决方案在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;
    }

If you're going with the solution of that post, then note that he's subscribing to the CellMouseDown event, not the MouseDown event. 如果您正在使用该帖子的解决方案,请注意他正在订阅CellMouseDown事件,而不是MouseDown事件。 This has a different signature. 这有不同的签名。

Also, as of .Net 2.0, you don't need all the delegate wrapping syntax, you can just += the function that matches the signature of the event delegate, like so: 此外,从.Net 2.0开始,您不需要所有委托包装语法,您只需+=与事件委托的签名匹配的函数,如下所示:

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

Then you won't have the error message, and can do what you see in the post. 然后您将没有错误消息,并且可以执行您在帖子中看到的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM