Click here to Skip to main content
15,889,216 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi All,

I have a DatagridView with Dock as Fill..
suppose there are only 5 Rows in Datagridview..

My need is to set Contextmenustrip within the Region of Rows..
ie., when user Right clicks outside the Area of Rows.. it should not show the Contextmenustrip..

Which event do i need to handle..?
(I tried Cellmousedown,cellclick,cellcontentclick --- But are not Working Perfectly)

Please help me on this..

Regards,
Pawan.
Posted

It is simple.

Try the following code block.

C#
private void dgvTestGrid_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
{
    if (e.Button == System.Windows.Forms.MouseButtons.Right)
    {
        if (dgvTableDescription.SelectedCells.Count > 0)
            MessageBox.Show("Ta..Da...");
            // Show the context menu instead of message box.
    }
}
 
Share this answer
 
Comments
Pawan Kiran 19-Oct-10 5:24am    
In Form_Load only it is Working..
suppose if u Leftclick on a Row and Leave All Rows in Datagridview and RightClick in other Area Of Datagridview, Contextmenustrip is appearing. but purpose is, it should not show the Contextmenustrip.

if u have more ideas on this.. please let me know.
Pawan Kiran 19-Oct-10 6:02am    
Hi Thanks to your post, i got the solution for my question..
Here is Solution..

C#
private void dgrid_MouseClick(object sender, MouseEventArgs e)
{
    dgrid.ContextMenuStrip = null;
}
private void dgrid_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
{
    dgrid.ContextMenuStrip = null;
}
private void dgrid_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
  if (e.RowIndex >= 0)
  {
     dgrid.ContextMenuStrip = contextMenuStrip2;
  }
}
 
Share this answer
 
v3
Comments
Rutvik Dave 20-Oct-10 16:04pm    
Yes, you are right. I haven't thought about that. Thanks for sharing the solution.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900