Hi,
You can simulate this by starting a new WinForm app.
Add a datagridview and add some columns.
Add a contextmenustrip, add a menuitem, doubleclick it to get a clickhandler.
Attach the menustrip to the datagridview in the properties window of the DGV.
Then refer to this code and the comments.

private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                // here we do not come although the contextmenustrip shows up under the mouse pointer
                this.dataGridView1.CurrentCell = this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];

                this.contextMenuStrip1.Show(this.dataGridView1, new Point(e.RowIndex, e.ColumnIndex));
            }
        }

        private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            
            if (e.Button == MouseButtons.Right)
            {
                // here we do come but with the added effect that the contextmenu shows up in he upper right
                // corner of the DGV before appearing under the mouse pointer as it should.
                this.dataGridView1.CurrentCell = this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];

                this.contextMenuStrip1.Show(this.dataGridView1, new Point(e.RowIndex, e.ColumnIndex));
            }
        }

        private void removeColumnToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // columnindex not always correct
            this.dataGridView1.Columns.RemoveAt(this.dataGridView1.CurrentCell.ColumnIndex);
        }

So with the mousedown I get the correct results with an extra menustrip popping up, with the mouseclick I get the correct menustrip but sometimes the wrong column gets deleted.
Can anyone explain what is happening, tried many things but I'm getting out of resources.
Any answer would be very much appreciated!

Edited 5 Years Ago by ddanbe: typo

Hi!

Do not use Show method of contextMenuStrip and no need to handle cell mouse events. In fact, Show() method parameters - ColumnIndex and RowIndex represent column and row index values not the region of datagridview location.

Here is code (from your snippet :) ).

private void Form1_Load(object sender, EventArgs e)
        {
            dataGridView1.ColumnCount = 4;
            dataGridView1.Columns[0].Name = "No1";
            dataGridView1.Columns[1].Name = "No2";
            dataGridView1.Columns[2].Name = "No3";
            dataGridView1.Columns[3].Name = "No4";

            dataGridView1.SelectionMode = DataGridViewSelectionMode.CellSelect;
            dataGridView1.CellMouseEnter += (sa, ea) =>
                {
                    if (ea.ColumnIndex != -1 && ea.RowIndex != -1)
                        dataGridView1.CurrentCell = dataGridView1.Rows[ea.RowIndex].Cells[ea.ColumnIndex];

                };
            testToolStripMenuItem.Click += (sa, ea) =>
                {
                    if (this.dataGridView1.CurrentCell != null)
                        this.dataGridView1.Columns.RemoveAt(this.dataGridView1.CurrentCell.ColumnIndex);

                    if (this.dataGridView1.CurrentCell == null)
                        dataGridView1.ContextMenuStrip = null;
                };
        }

Please let me know if you have any questions.

Comments
Showing deep knowledge!!!

Adatapost, amazing :-O
Thank you, thank you.
This shows off some deep knowledge.
I definitely have to work myself into LINQ I guess.:-/
One question: what does sa in your code mean?

Datagridview right click and delete row...

    private void dataGridView1_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
    {
        if (e.Button == MouseButtons.Right) 
        {
            this.dataGridView1.Rows[e.RowIndex].Selected = true; 
            this.rowIndex = e.RowIndex;
            this.dataGridView1.CurrentCell = this.dataGridView1.Rows[e.RowIndex].Cells[1];
            this.contextMenuStrip1.Show(this.dataGridView1, e.Location);
            contextMenuStrip1.Show(Cursor.Position);
        }
    }

Source...Right click and delete from datagridview

watson

how to code datagridview with a msaccess database that you can add data in datagrid...update and delete data??

Edited 2 Years Ago by kira1220

This question has already been answered. Start a new discussion instead.