Creating Context Menu for Data Grid

We can create context menu for the Data Grid cell here is the code to create the context menu and to assign the event..


// creating Context menu for data grid.
ToolStripMenuItem mnuStart = new ToolStripMenuItem("Start");
ToolStripMenuItem mnuStartAll = new ToolStripMenuItem("StartAll");
ToolStripMenuItem mnuPause = new ToolStripMenuItem("Pause");
ToolStripMenuItem mnuDelete = new ToolStripMenuItem("Delete");

mnuStart.Click += new EventHandler(mnuStart_Click); // creating the event handeler for the context menu.
mnuStartAll.Click += new EventHandler(mnuStartAll_Click);
mnuPause.Click += new EventHandler(mnuPause_Click);
mnuDelete.Click += new EventHandler(mnuDelete_Click);

//Add to main context menu
mnu.Items.AddRange(new ToolStripItem[] { mnuStart, mnuStartAll, mnuStartAll, mnuPause, mnuDelete });


and for assigning it for the Mouse Right Click please use the following code..



private void dgFile_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
if (dgFile.Rows.Count == 0)
{
dgFile.ContextMenuStrip = null;
}
else
{
dgFile.ContextMenuStrip = mnu;
}
}
}


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: