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.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
muhammad hammdPosted Mar 10, 2011, 4:04 AM
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);
}
}
}
Sheethal R APosted Sep 24, 2010, 12:07 AM
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.
Dorababu MekaPosted Sep 23, 2010, 4:51 AM
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);
}
}
Dorababu MekaPosted Sep 23, 2010, 4:37 AM
Loop and find the number of rows you are having in grid.
On mousedown event call the context menu on the row you clicked