Hi,
I need to add a context menu on right click of a data grid row only(not all cells).
Thanks,
Sheethal
Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Dorababu MekaRank #457 · 5.5K reputationPosted Sep 23, 2010, 12:37 AM EDT
Loop and find the number of rows you are having in grid.
On mousedown event call the context menu on the row you clicked
Dorababu MekaRank #457 · 5.5K reputationPosted Sep 23, 2010, 12:51 AM EDT
private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
DataGridView.HitTestInfo hti = dataGridView1.HitTest(e.X, e.Y);
dataGridView1.ClearSelection();
dataGridView1.Rows[hti.RowIndex].Selected = true;
contextMenu.Show(dataGridView1,e.Location);
}
}
Sheethal R A11 reputationPosted Sep 23, 2010, 8:07 PM EDT
Thanks a lot its working..
With this code its working for any cell click.
But I want context menu only for Row Click... Can u let me know how tat can be done...
Thanks in advance.
muhammad hammd2 reputationPosted Mar 9, 2011, 11:04 PM EST
u can add context menu on right click of a data grid row only as next
use the event mouse up
you should define your datagridview and your context menue
// here the event of mouse up
private void dataGridView1_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Right)
{
System.Windows.Forms.DataGridView.HitTestInfo myHitTest;
myHitTest = dataGridView1.HitTest(e.X, e.Y);
if (myHitTest.Type == DataGridViewHitTestType.RowHeader)
{
contextMenuStrip1.Show(dataGridView1, e.Location);
}
}
}