John M.
John M.

Reputation: 91

dataGridView Context menu function

I have added this code to my forum:

    private void dataGridView1_MouseClick(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Right)
        {
            ContextMenu a = new ContextMenu();
            a.MenuItems.Add(new MenuItem("Google"));
            a.MenuItems.Add(new MenuItem("Yahoo"));
            int currentMouseOverRow = dataGridView1.HitTest(e.X, e.Y).RowIndex;
            if (currentMouseOverRow >= 0)
            {
                a.MenuItems.Add(new MenuItem(string.Format("", currentMouseOverRow.ToString())));
            }
            a.Show(dataGridView1, new Point(e.X, e.Y));
        }
    }

    private void dataGridView1_CellMouseEnter(object sender, DataGridViewCellEventArgs e)
    {
        currentMouseOverRow = e.RowIndex;
    }

    private void dataGridView1_CellMouseLeave(object sender, DataGridViewCellEventArgs e)
    {
        currentMouseOverRow = -1;
    }

But how do I add the function to the context menu option?

For google,

    Process.Start("https://www.google.com");

For yahoo,

    Process.Start("https://www.yahoo.com");

etc.

Upvotes: 2

Views: 5886

Answers (2)

Edper
Edper

Reputation: 9322

Why not add your ContextMenu right when your Form loads rather than every time that the user right click your DataGridView which means that you have to add the Context Menu every time the user rights click your DatGridView.

Secondly, instead of ContextMenu make a ContextMenuStrip instead which more at home with DataGridView. So, your code would look like:

ContextMenuStrip a = new ContextMenuStrip();

public Form1()
{
   InitializeComponent();
   this.Load += new EventHandler(Form1_Load);
}

void Form1_Load(object sender, EventArgs e)
{
   Image img = null;
   a.Items.Add("Google", img, new System.EventHandler(ContextMenuClick));
   a.Items.Add("Yahoo", img, new System.EventHandler(ContextMenuClick));
   dataGridView1.ContextMenuStrip = a;
}

Then your EventHandler would look like this:

private void ContextMenuClick(Object sender, System.EventArgs e)
 {
   switch (sender.ToString().Trim())
    {
      case "Google":
        Process.Start("https://www.google.com");
        break;
      case "Yahoo":
        Process.Start("https://www.yahoo.com");
        break;
    }
 }

And your DataGridView Mouse Click handler would look this:

void dataGridView1_MouseClick(object sender, MouseEventArgs e)
 {
   if (e.Button == MouseButtons.Right)
     {
       int currentMouseOverRow = dataGridView1.HitTest(e.X, e.Y).RowIndex;
       if (currentMouseOverRow >= 0)
         {
           a.Items.Add(string.Format("", currentMouseOverRow.ToString()));
         }
       a.Show(dataGridView1, new Point(e.X, e.Y));
     }
 }

Upvotes: 1

gzaxx
gzaxx

Reputation: 17600

You have to use ClickEvent for your menu items:

//menu items constructor
a.MenuItems.Add(new MenuItem("Google", new System.EventHandler(this.MenuItemClick)));
a.MenuItems.Add(new MenuItem("Yahoo", new System.EventHandler(this.MenuItemClick)));

private void MenuItemClick(Object sender, System.EventArgs e)
{
     var m = (MenuItem)sender;

     if (m.Text == "Google")
     {
         Process.Start("https://www.google.com");
     }
}

Upvotes: 1

Related Questions