HitTestInfo.RowIndex never seems to change?

I'm using a ContextMenuStrip control on a DataGridView. When a User right-clicks the Mouse, the menu will popup. In the process, the HitTestInfo.RowIndex value is captured identifying the row that was clicked on. Problem is, I assign the HitTestInfo.RowIndex value to a variable but the value seems to never change. Below is my code:

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

            If e.Button = MouseButtons.Right Then
                hitTestInfo = dgvStandardTestData.HitTest(e.X, e.Y)
                dgvStandardTestData.ContextMenuStrip = cms1

                idx = hitTestInfo.RowIndex 'This will retrieve the index of the Row clicked on

                If hitTestInfo.Type = DataGridViewHitTestType.RowHeader Then
                    If Me.dgvStandardTestData.Rows.Count > 1 Then
                        Me.dgvStandardTestData.Rows(hitTestInfo.RowIndex).Selected = True
                        If hitTestInfo.ColumnIndex = -1 Then
                            cms1.Show(dgvStandardTestData, New Point(e.X, e.Y))
                        End If
                    End If
                End If
            End If

            hitTestInfo = Nothing

        Catch ex As Exception
            strErr = gfrmID & "/dgvStandardTestData_MouseClick() - " & ex.Message
            MessageBox.Show(strErr, "User Notification", MessageBoxButtons.OK)
        End Try
    End Sub
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.

Robert SchuttSoftware EngineerCommented:
In your event code you set
dgvStandardTestData.ContextMenuStrip = cms1

Open in new window

After this, the event is not called anymore but rather default dgv code to show cms1 (you can check this by putting a breakpoint in). You could change this to show cms1 on cell click manually same as you do on rowheader click:
    Private Sub dgvStandardTestData_MouseClick(sender As Object, e As MouseEventArgs) Handles dgvStandardTestData.MouseClick
        Try
            Dim hitTestInfo As DataGridView.HitTestInfo

            If e.Button = MouseButtons.Right Then
                hitTestInfo = dgvStandardTestData.HitTest(e.X, e.Y)
                'dgvStandardTestData.ContextMenuStrip = cms1

                idx = hitTestInfo.RowIndex 'This will retrieve the index of the Row clicked on

                If hitTestInfo.Type = DataGridViewHitTestType.Cell Then
                    cms1.Show(dgvStandardTestData, New Point(e.X, e.Y))
                ElseIf hitTestInfo.Type = DataGridViewHitTestType.RowHeader Then
                    If Me.dgvStandardTestData.Rows.Count > 1 Then
                        Me.dgvStandardTestData.Rows(hitTestInfo.RowIndex).Selected = True
                        If hitTestInfo.ColumnIndex = -1 Then
                            cms1.Show(dgvStandardTestData, New Point(e.X, e.Y))
                        End If
                    End If
                End If
            End If

            hitTestInfo = Nothing

        Catch ex As Exception
            strErr = gfrmID & "/dgvStandardTestData_MouseClick() - " & ex.Message
            MessageBox.Show(strErr, "User Notification", MessageBoxButtons.OK)
        End Try
    End Sub

Open in new window

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:
Do you mean like this:

        Private Sub dgvStandardTestData_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgvStandardTestData.CellClick
        Try
            If e.ColumnIndex = -1 Then
                dgvStandardTestData.ContextMenuStrip = cms1
            End If

        Catch ex As Exception
            strErr = gfrmID & "/dgvStandardTestData_CellClick() - " & ex.Message
            MessageBox.Show(strErr, "User Notification", MessageBoxButtons.OK)
        End Try
    End Sub
0
Robert SchuttSoftware EngineerCommented:
No, that's something completely different.

Why would you want to set dgvStandardTestData.ContextMenuStrip in an event like this? It changes how your program works from that moment on. There are uses for it, but in this case it seems to me you should set it at the start of the program (and then don't use click events) or leave it unset and use code like in my previous post. Did that not work for you?
0
PMI ACP® Project Management

Prepare for the PMI Agile Certified Practitioner (PMI-ACP)® exam, which formally recognizes your knowledge of agile principles and your skill with agile techniques.

BlakeMcKennaAuthor Commented:
Your code works, I can now capture the RowIndex, however, if a User clicks on any other cell in the Grid...the PopUp Menu still pops up. How can I limit it to just appearing when the User clicks on Column -1?
0
Robert SchuttSoftware EngineerCommented:
Ah, now I see. Well just leave out that first 'if' I added because I thought you wanted to show the menu always:
    Private Sub dgvStandardTestData_MouseClick(sender As Object, e As MouseEventArgs) Handles dgvStandardTestData.MouseClick
        Try
            Dim hitTestInfo As DataGridView.HitTestInfo

            If e.Button = MouseButtons.Right Then
                hitTestInfo = dgvStandardTestData.HitTest(e.X, e.Y)
                'dgvStandardTestData.ContextMenuStrip = cms1

                idx = hitTestInfo.RowIndex 'This will retrieve the index of the Row clicked on

                If hitTestInfo.Type = DataGridViewHitTestType.RowHeader Then
                    If Me.dgvStandardTestData.Rows.Count > 1 Then
                        Me.dgvStandardTestData.Rows(hitTestInfo.RowIndex).Selected = True
                        If hitTestInfo.ColumnIndex = -1 Then
                            cms1.Show(dgvStandardTestData, New Point(e.X, e.Y))
                        End If
                    End If
                End If
            End If

            hitTestInfo = Nothing

        Catch ex As Exception
            strErr = gfrmID & "/dgvStandardTestData_MouseClick() - " & ex.Message
            MessageBox.Show(strErr, "User Notification", MessageBoxButtons.OK)
        End Try
    End Sub

Open in new window

0
BlakeMcKennaAuthor Commented:
I actually did that and and that worked!

Thank you for your help!
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.