C# DataGridView Right Click to ContextMenu Click Retrieve Cell Value

Matt picture Matt · Jun 21, 2013 · Viewed 17.9k times · Source

I have a DataGridView. I have created a ContextMenuStrip when Right Clicking a Cell in column 4 of my DataGridView. 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.

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. 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, DataGridViewCellMouseEventArgs e)

I am not sure how to change 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

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;
    }

Answer

Gjeltema picture Gjeltema · Jun 21, 2013

If you're going with the solution of that post, then note that he's subscribing to the CellMouseDown event, not the MouseDown event. 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:

// 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.