Thursday, June 25, 2009

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;
dgFile.ContextMenuStrip = mnu;
Anil Kumar Pandey
System Architect
Green Point Technology (India) Ltd.
Mumbai, Maharshtra