Link to home
Start Free TrialLog in
Avatar of BlakeMcKenna
BlakeMcKennaFlag for United States of America

asked on

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?
Avatar of John (Yiannis) Toutountzoglou
John (Yiannis) Toutountzoglou
Flag of Greece image

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
Avatar of BlakeMcKenna

ASKER

I tried your code and the 2nd If statement condition is never met.  The hitTestInfo.Type value is always 0.
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?
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...
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 ?
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...
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!
ASKER CERTIFIED SOLUTION
Avatar of John (Yiannis) Toutountzoglou
John (Yiannis) Toutountzoglou
Flag of Greece image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
    .
    .
    .
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

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