Click here to Skip to main content
15,888,282 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am using a context menu on a datagridview form. I am trying to find what item was clicked on in the context menu.

What I have tried:

C#
using WinForms = System.Windows.Forms;

private void dgvExcel_MouseDown(object sender, WinForms.MouseEventArgs e)
{
    // Define the context menu
    WinForms.ContextMenu EditMenu = new WinForms.ContextMenu();
    if (e.Button == WinForms.MouseButtons.Right)
    {
        // There was a right-click on a cell, so add the items to the list
        EditMenu.MenuItems.Add(new WinForms.MenuItem("Edit"));
        EditMenu.MenuItems.Add(new WinForms.MenuItem("Copy"));
        EditMenu.MenuItems.Add(new WinForms.MenuItem("Delete"));

        // A cell received a right click, so show the context menu
        EditMenu.Show(dgvExcel, new System.Drawing.Point(e.X, e.Y));
    }
    //I have tried
    if (WinForms.MenuItem.Equals("Edit"))
    if (WinForms.MenuItem.Equals(0))
    if (EditMenu.Equals(0)
}
Posted
Updated 26-Jul-23 4:46am
Comments
Member 15627495 26-Jul-23 10:31am    
Hi,


- check type of sender , if it is datagrid
- create a 'temp' obj of type datagrid ( to use all features of datagrid, the var is a copy of the one datagrid. use a cast to convert 'sender' to datagrid Type.
- get the index of rows / columns / or cell ..
- pick the value

Add event handlers:
C#
private void MyDataGridView_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
    {
    ContextMenu EditMenu = new ContextMenu();
    if (e.Button == MouseButtons.Right)
        {
        // There was a right-click on a cell, so add the items to the list
        EditMenu.MenuItems.Add(new MenuItem("Edit", Edit_Click));
        EditMenu.MenuItems.Add(new MenuItem("Copy", Copy_Click));
        EditMenu.MenuItems.Add(new MenuItem("Delete", Delete_Click));

        // A cell received a right click, so show the context menu
        EditMenu.Show(MyDataGridView, new System.Drawing.Point(e.X, e.Y));
        }

    }
private void Edit_Click(object sender, EventArgs e)
    {
    Debug.WriteLine("Edit");
    }
private void Copy_Click(object sender, EventArgs e)
    {
    Debug.WriteLine("Copy");
    }
private void Delete_Click(object sender, EventArgs e)
    {
    Debug.WriteLine("Delete");
    }
 
Share this answer
 
This is a Microsoft example: DataGridViewRow.ContextMenuStrip Property (System.Windows.Forms) | Microsoft Learn[^]

Create a new project, drop a DataGridView control on a form, then add the following code:
C#
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        InitData();
    }

    public List<Person> People { get; set; } = new List<Person>
    {
        new Person { Age = 21, FirstName = "Paul", LastName = "McCartney" },
        new Person { Age = 22, FirstName = "John", LastName = "Lennon" },
        new Person { Age = 23, FirstName = "George", LastName = "Harrison" },
        new Person { Age = 24, FirstName = "Ringo", LastName = "Starr" },
    };

    private void InitData()
    {
        dataGridView1.MultiSelect = false;
        dataGridView1.DataSource = People;
        dataGridView1.Rows[0].Cells[0].Selected = true;

        AddContextMenu();
        dataGridView1.CellMouseEnter += dataGridView_CellMouseEnter;
    }

    ToolStripMenuItem toolStripItem1 = new ToolStripMenuItem();

    private void AddContextMenu()
    {
        toolStripItem1.Text = "Redden";
        toolStripItem1.Click += new EventHandler(toolStripItem1_Click);
        ContextMenuStrip strip = new ContextMenuStrip();
        foreach (DataGridViewColumn column in dataGridView1.Columns)
        {

            column.ContextMenuStrip = strip;
            column.ContextMenuStrip.Items.Add(toolStripItem1);
        }
    }

    private DataGridViewCellEventArgs mouseLocation;

// Change the cell's color.
    private void toolStripItem1_Click(object sender, EventArgs args)
    {
        dataGridView1.Rows[mouseLocation.RowIndex]
                .Cells[mouseLocation.ColumnIndex].Style.BackColor
            = Color.Red;
    }

// Deal with hovering over a cell.
    private void dataGridView_CellMouseEnter(object sender,
        DataGridViewCellEventArgs location)
    {
        mouseLocation = location;
    }
}

public class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int Age { get; set; }
}

Now run and right-click on any cell. A context menu is shown and you can select the item and the click event is captured.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900