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 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));
}
}

imad ahmedPosted Jan 16, 2013, 2:18 AM
hello Nipun Tomar,i m using u r code for Creating a Context Menu on a DataGridView Mouse Click but i m facin error Error 3 No overload for method 'Show' takes '2' arguments in contextMenuForColumn1.Show(dataGridView, new Point(e.X, e.Y)); pllzz help me to fix this error i will very thanx full to u.
Ranjeet BaranwalPosted Aug 2, 2010, 2:07 AM
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 { } }
kamal dewanganPosted Apr 27, 2010, 3:00 PM
help me click grid and show select row in textbox plssssssssssssssss........ help me E-mail [email protected]
nipul peditedPosted Feb 24, 2010, 7:17 AMEdited Feb 24, 2010, 7:18 AM
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.