SIGN UP MEMBER LOGIN:    
ARTICLE

Creating a Context Menu on a DataGridView Mouse Click

Posted by Nipun Tomar Articles | Windows Forms C# January 17, 2007
This article describes how to create a context menu when you mouse click on a DataGridView control in Windows Forms.
Reader Level:

One of the projects had a requirement to create a context menu on mouse click over a DataGridView having employee details. The menu items may vary from column to column of the gridview.

//Define different context menus for different columns
private ContextMenu contextMenuForColumn1 = new ContextMenu();
private ContextMenu contextMenuForColumn2 = new ContextMenu();

Add the following line of code in the form load event:

private void Form_Load(object sender, EventArgs e)
{
    // Load all default values of controls
    populateDataGridView();

    // Add context mneu items
    contextMenuForColumn1.MenuItems.Add("Make Active", new     EventHandler(MakeActive));
    contextMenuForColumn2.MenuItems.Add(
"Delete", new     EventHandler(Delete));
    contextMenuForColumn2.MenuItems.Add(
"Register", new     EventHandler(Register));
}

Add the following code to mouseup event of the gridview:

private void dataGridView_MouseUp(object sender, MouseEventArgs e)
{
    // Load context menu on right mouse click
    DataGridView.HitTestInfo hitTestInfo;
    if (e.Button == MouseButtons.Right)
    {
        hitTestInfo = dataGridView.HitTest(e.X, e.Y);
        // If column is first column
        if (hitTestInfo.Type == DataGridViewHitTestType.Cell && hitTestInfo.ColumnIndex == 0)
            contextMenuForColumn1.Show(dataGridView,
new Point(e.X, e.Y));
        // If column is second column
        if (hitTestInfo.Type == DataGridViewHitTestType.Cell && hitTestInfo.ColumnIndex == 1)
            contextMenuForColumn2.Show(dataGridView,
new Point(e.X, e.Y));
    }
}
 

share this article :
post comment
 

private void dgvSchedule_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)

{

try

{

if (e.Button == MouseButtons.Right)

{

this.dgvSchedule.CurrentCell = this.dgvSchedule[e.ColumnIndex, e.RowIndex];

}

}

catch (Exception ex)

{

clsMessage.ShowErrorMessage("Error: " + ex.Message);

}

finally

{

}

}

Posted by Ranjeet Baranwal Aug 02, 2010

help me click grid and show select row in textbox
plssssssssssssssss........ help me
E-mail ==kamal.kumar668@gmail.com

Posted by kamal dewangan Apr 27, 2010

Hi,

I am developing contextmenu for datagrid. When User click on any one menu out of that context menu list the whole detail with a particular user need to be open using it unique ID from database.
For That i create context menu but failed to get particular user details. I working in vb.net.


Please help me to solve this. if poss please put a code for this.

Thanks in advance.

Posted by nipul p Feb 24, 2010
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Team Foundation Server Hosting
Become a Sponsor