Solved

SHOW CONTEXTMENUSTRIP AT CURRENT CELL POSITION

Posted on 2011-03-14
19
809 Views
Last Modified: 2012-05-11
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
Comment
Question by:emi_sastra
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 9
19 Comments
 
LVL 19

Expert Comment

by:Shahan Ayyub
ID: 35141377
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
 
LVL 1

Author Comment

by:emi_sastra
ID: 35142550
Hi Shahan_Dev,

I want to use mouse double click.

Thank you.
0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 35143028
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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 35143097
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
 
LVL 1

Author Comment

by:emi_sastra
ID: 35143121
Hi jtoutou,

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

Thank you.
0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 35143143
See my second post
0
 
LVL 1

Author Comment

by:emi_sastra
ID: 35143144
Your second solution always shown at column header.

Thank you.
0
 
LVL 1

Author Comment

by:emi_sastra
ID: 35143154
Hi Shahan_Dev,

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

Thank you.
0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 35143157
No it show at the current cell ...did you change the cell mouse click in just MouseDoubleClick event ...it works fine for me...
0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 35143173
emi my solution works ...note tha you have to left double click your mouse...
also check the event ..is mouse doubleclick...
0
 
LVL 1

Author Comment

by:emi_sastra
ID: 35143198
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
 
LVL 18

Accepted Solution

by:
John (Yiannis) Toutountzoglou earned 500 total points
ID: 35143215
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
 
LVL 1

Author Comment

by:emi_sastra
ID: 35143225
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
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 35143248
please can you tell me which event are u using??
0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 35143258
Also with selection mode to full row select it works....check again once more "dgvHeader_MouseDoubleClick" not CellDoubleClick
0
 
LVL 1

Author Comment

by:emi_sastra
ID: 35143262
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
 
LVL 1

Author Comment

by:emi_sastra
ID: 35143265
Oops, let me try it.

Thank you.
0
 
LVL 1

Author Comment

by:emi_sastra
ID: 35143303
It works.

Thank you very much for your help.
0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 35143314
Glad I help you ..!!!
0

Featured Post

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Footer for each row on Gridview 2 41
Help with exporting to excel 4 45
Help with opening combobox using VB.NET. 3 38
Passing data between Forms 3 28
Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question