• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 814
  • Last Modified:

SHOW CONTEXTMENUSTRIP AT CURRENT CELL POSITION

Hi All,

I want to achieve above task.

What should I do ?

Thank you.
Private Sub dgvHeader_CellMouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles dgvHeader.CellMouseDoubleClick

        Me.ContextMenuStrip1.Show(dgvHeader, New Point(dgvHeader.Width / 2, _
                                                dgvHeader.CurrentCell.GetContentBounds(e.RowIndex).Y))
    End Sub

Open in new window

0
emi_sastra
Asked:
emi_sastra
  • 9
  • 9
1 Solution
 
Shahan AyyubSenior Software Engineer - iOSCommented:
Hi!

Adding this to your Form_Load Event will do the job:
Me.DataGridView1.ContextMenuStrip = ContextMenuStrip1

Open in new window


Whenever user will press the right mouse button, menu will be show up at the current cell.

In case, this solution doesn't help you. Please elaborate more.
0
 
emi_sastraAuthor Commented:
Hi Shahan_Dev,

I want to use mouse double click.

Thank you.
0
 
John (Yiannis) ToutountzoglouCommented:
Hi....
try this in your celldouble Click event ....just adjust your coordinates according to your needs..
       
 Dim x As Integer
        Dim y As Integer
        x = Me.MyDataGridView.CurrentCell.ContentBounds.Left    ' or the  "x" position you need
        y = Me.My.CurrentCell.ContentBounds.Right  ' or the  "y" position you need
        Dim xy As New Point(x, y)
        ContextMenuStrip1.Show(Me, xy)

Open in new window


Hope it helps
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
John (Yiannis) ToutountzoglouCommented:
Sorry My first post contains mistakes.....This is what you want (Maybe).....(just use Mouse Double click)

Private Sub MyDataGridView_MouseDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyDataGridView.MouseDoubleClick
        Dim hitTestInfo As DataGridView.HitTestInfo
        If e.Button = MouseButtons.Left Then
            hitTestInfo = MyDataGridView.HitTest(e.X, e.Y)
                    If hitTestInfo.Type = DataGridViewHitTestType.Cell Then
                ContextMenu1.Show(MyDataGridView, New Point(e.X, e.Y))
            End If
        End If
    End Sub

Open in new window

0
 
emi_sastraAuthor Commented:
Hi jtoutou,

It always show at header row position, should be at the current cell position.

Thank you.
0
 
John (Yiannis) ToutountzoglouCommented:
See my second post
0
 
emi_sastraAuthor Commented:
Your second solution always shown at column header.

Thank you.
0
 
emi_sastraAuthor Commented:
Hi Shahan_Dev,

The bad news using your solution is when we click outside the grid, the menustrip still shown.

Thank you.
0
 
John (Yiannis) ToutountzoglouCommented:
No it show at the current cell ...did you change the cell mouse click in just MouseDoubleClick event ...it works fine for me...
0
 
John (Yiannis) ToutountzoglouCommented:
emi my solution works ...note tha you have to left double click your mouse...
also check the event ..is mouse doubleclick...
0
 
emi_sastraAuthor Commented:
Please see below code.

Private Sub dgvHeader_CellMouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles dgvHeader.CellMouseDoubleClick

   
        Me.ContextMenuStrip1.Show(dgvHeader, New Point(e.X, e.Y))


    End Sub

Anything wrong ?

Thank you.
0
 
John (Yiannis) ToutountzoglouCommented:
just copy the code exactly as it is...and paste it to your code....
Private Sub dgvHeader_MouseDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles dgvHeader.MouseDoubleClick
        Dim hitTestInfo As DataGridView.HitTestInfo
        If e.Button = MouseButtons.Left Then
            hitTestInfo = dgvHeader.HitTest(e.X, e.Y)
                    If hitTestInfo.Type = DataGridViewHitTestType.Cell Then
                ContextMenuStrip1.Show(dgvHeader, New Point(e.X, e.Y))
            End If
        End If
    End Sub

Open in new window

0
 
emi_sastraAuthor Commented:
With FullRowSelect of Selection Mode, your code will not work at all.

With the simple code above then it shows at header column.

Thank you.
0
 
John (Yiannis) ToutountzoglouCommented:
please can you tell me which event are u using??
0
 
John (Yiannis) ToutountzoglouCommented:
Also with selection mode to full row select it works....check again once more "dgvHeader_MouseDoubleClick" not CellDoubleClick
0
 
emi_sastraAuthor Commented:
I have tried both

Private Sub dgvHeader_CellMouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles dgvHeader.CellMouseDoubleClick

   
        Me.ContextMenuStrip1.Show(dgvHeader, New Point(e.X, e.Y))


    End Sub

1. Complete code, with FullRowSelect of Selection Mode property, your code will not work at all.
2. Simple code, then it shows at header column.

Thank you.
0
 
emi_sastraAuthor Commented:
Oops, let me try it.

Thank you.
0
 
emi_sastraAuthor Commented:
It works.

Thank you very much for your help.
0
 
John (Yiannis) ToutountzoglouCommented:
Glad I help you ..!!!
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 9
  • 9
Tackle projects and never again get stuck behind a technical roadblock.
Join Now