DataGridView and ContextMenuStrip?

I searched through the archives and found a post that was almost exactly what I was looking for, however, it didn't do what I was expecting. I have a ContextMenuStrip attached to a DataGridView. What I need to be able to do is right-click on a row in my DGV, the ContextMenu will popup and the user can make a selection. My problem is that I need to capture the row index of the DGV so that the Menu Option selected can take the appropriate action using that row of data. I'm just not sure what event to use to do this. I've never used a ContextMenuStrip control before. Do I look in the CMS Events or the DGV Events?
BlakeMcKennaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

John (Yiannis) ToutountzoglouInstructor Multiengine PilotCommented:
HI ...

Here is a sample...
 Dim HitinfoStatus As Integer
    Private Sub DatagridView1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DatagridView1.MouseClick

        Dim hitTestInfo As DataGridView.HitTestInfo
        If e.Button = MouseButtons.Right Then
            hitTestInfo = DatagridView1.HitTest(e.X, e.Y)
            If hitTestInfo.Type = DataGridViewHitTestType.Cell Then
                If hitTestInfo.ColumnIndex > 1 Then
                    Me.DatagridView1.Rows(hitTestInfo.RowIndex).Selected = True
                    If Me.DatagridView1.Rows.Count > 1 Then
                        ContextMenu1.Enabled = True
                    Else
                        ContextMenu1.Enabled = False
                    End If
                    ContextMenu1.Show(DatagridView1, New Point(e.X, e.Y))
                    HitinfoStatus = hitTestInfo.RowIndex
                End If
            End If
        End If
    End Sub

Open in new window


Hope it helps

Yiannis
0
BlakeMcKennaAuthor Commented:
I tried your code and the 2nd If statement condition is never met.  The hitTestInfo.Type value is always 0.
0
John (Yiannis) ToutountzoglouInstructor Multiengine PilotCommented:
Yes you are right this is for my needs ..Did you try with
   If hitTestInfo.ColumnIndex >=0 

Open in new window


Can you please post what you got till now?
0
Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

BlakeMcKennaAuthor Commented:
This is the test I'm referring too.

    If hitTestInfo.Type = DataGridViewHitTestType.Cell Then

The value of "hitTestInfo.Type" is ALWAYS 0 and the value of DataGridViewHitTestType.Cell is ALWAYS 1 no matter where I click in the grid...
0
John (Yiannis) ToutountzoglouInstructor Multiengine PilotCommented:
The 0  is due to DataGridViewHitTestType Enumeration
Check here here

If you want to capture the row index hitTestInfo.RowIndexis what you want...

Does your context  menu open ?
0
BlakeMcKennaAuthor Commented:
I apologize, you are right. I overlooked something in your code. DataGridView1 needs to be the name of the DGV that I am using. That was my problem...
0
BlakeMcKennaAuthor Commented:
I've requested that this question be closed as follows:

Accepted answer: 0 points for BlakeMcKenna's comment #a39497128

for the following reason:

Thank you for your help!
0
John (Yiannis) ToutountzoglouInstructor Multiengine PilotCommented:
Didn't you find my posts helpful?
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
BlakeMcKennaAuthor Commented:
Yes, I did. Again I apologize for my mistake of not seeing the DataGridView1. My only issue now is that I want the CMS to popup only when the RowHeader is clicked on and not any other column. Currently, it pops up on any column. I know your code specified a check of this:

    If hitTestInfo.ColumnIndex > 1 Then
    .
    .
    .

I ended up coding it to

    If hitTestInfo.ColumnIndex = -1 Then     'Only pops up when RowHeader is clicked on
    .
    .
    .
0
John (Yiannis) ToutountzoglouInstructor Multiengine PilotCommented:
I want the CMS to popup only when the RowHeader is clicked
just change the Type
 If hitTestInfo.Type = DataGridViewHitTestType.RowHeader

Open in new window


Your Final Code should be :
 Dim HitinfoStatus As Integer
    Private Sub DatagridView1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DataGridView1.MouseClick

        Dim hitTestInfo As DataGridView.HitTestInfo
        If e.Button = MouseButtons.Right Then
            hitTestInfo = DatagridView1.HitTest(e.X, e.Y)
            If hitTestInfo.Type = DataGridViewHitTestType.RowHeader Then

                Me.DataGridView1.Rows(hitTestInfo.RowIndex).Selected = True
                If Me.DataGridView1.Rows.Count > 1 Then
                    ContextMenu1.Enabled = True
                Else
                    ContextMenu1.Enabled = False
                End If
                ContextMenu1.Show(DataGridView1, New Point(e.X, e.Y))
                HitinfoStatus = hitTestInfo.RowIndex

            End If
        End If
    End Sub

Open in new window

0
BlakeMcKennaAuthor Commented:
I've tried that too. Here is what I have so far.

        Private Sub dgvStandardTestData_MouseClick(sender As Object, e As MouseEventArgs) Handles dgvStandardTestData.MouseClick
        Try
            Dim hitTestInfo As DataGridView.HitTestInfo

            idx = 0

            If e.Button = MouseButtons.Right Then
                hitTestInfo = dgvStandardTestData.HitTest(e.X, e.Y)
                dgvStandardTestData.ContextMenuStrip = cms1
                If hitTestInfo.Type = DataGridViewHitTestType.RowHeader Then
                    Dim rowSelected As DataGridViewRow = dgvStandardTestData.CurrentRow
                    If Me.dgvStandardTestData.Rows.Count > 1 Then
                        Me.dgvStandardTestData.Rows(hitTestInfo.RowIndex).Selected = True
                        If hitTestInfo.ColumnIndex = -1 Then
                            idx = hitTestInfo.RowIndex  'This will retrieve the index of the Row clicked on
                            cms1.Enabled = True
                            cms1.Show(dgvStandardTestData, New Point(e.X, e.Y))
                        Else
                            cms1.Hide()
                        End If
                    End If
                End If
            End If

        Catch ex As Exception
            strErr = gfrmID & "/dgvStandardTestData_MouseClick() - " & ex.Message
            MessageBox.Show(strErr, "User Notification", MessageBoxButtons.OK)
        End Try
    End Sub
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.