BlakeMcKenna
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?
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
Can you please post what you got till now?
If hitTestInfo.ColumnIndex >=0
Can you please post what you got till now?
ASKER
This is the test I'm referring too.
If hitTestInfo.Type = DataGridViewHitTestType.Ce ll Then
The value of "hitTestInfo.Type" is ALWAYS 0 and the value of DataGridViewHitTestType.Ce ll is ALWAYS 1 no matter where I click in the grid...
If hitTestInfo.Type = DataGridViewHitTestType.Ce
The value of "hitTestInfo.Type" is ALWAYS 0 and the value of DataGridViewHitTestType.Ce
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 ?
Check here here
If you want to capture the row index hitTestInfo.RowIndexis what you want...
Does your context menu open ?
ASKER
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...
ASKER
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!
Accepted answer: 0 points for BlakeMcKenna's comment #a39497128
for the following reason:
Thank you for your help!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
.
.
.
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 clickedjust change the Type
If hitTestInfo.Type = DataGridViewHitTestType.RowHeader
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
ASKER
I've tried that too. Here is what I have so far.
Private Sub dgvStandardTestData_MouseC lick(sende r As Object, e As MouseEventArgs) Handles dgvStandardTestData.MouseC lick
Try
Dim hitTestInfo As DataGridView.HitTestInfo
idx = 0
If e.Button = MouseButtons.Right Then
hitTestInfo = dgvStandardTestData.HitTes t(e.X, e.Y)
dgvStandardTestData.Contex tMenuStrip = cms1
If hitTestInfo.Type = DataGridViewHitTestType.Ro wHeader Then
Dim rowSelected As DataGridViewRow = dgvStandardTestData.Curren tRow
If Me.dgvStandardTestData.Row s.Count > 1 Then
Me.dgvStandardTestData.Row s(hitTestI nfo.RowInd ex).Select ed = True
If hitTestInfo.ColumnIndex = -1 Then
idx = hitTestInfo.RowIndex 'This will retrieve the index of the Row clicked on
cms1.Enabled = True
cms1.Show(dgvStandardTestD ata, New Point(e.X, e.Y))
Else
cms1.Hide()
End If
End If
End If
End If
Catch ex As Exception
strErr = gfrmID & "/dgvStandardTestData_Mous eClick() - " & ex.Message
MessageBox.Show(strErr, "User Notification", MessageBoxButtons.OK)
End Try
End Sub
Private Sub dgvStandardTestData_MouseC
Try
Dim hitTestInfo As DataGridView.HitTestInfo
idx = 0
If e.Button = MouseButtons.Right Then
hitTestInfo = dgvStandardTestData.HitTes
dgvStandardTestData.Contex
If hitTestInfo.Type = DataGridViewHitTestType.Ro
Dim rowSelected As DataGridViewRow = dgvStandardTestData.Curren
If Me.dgvStandardTestData.Row
Me.dgvStandardTestData.Row
If hitTestInfo.ColumnIndex = -1 Then
idx = hitTestInfo.RowIndex 'This will retrieve the index of the Row clicked on
cms1.Enabled = True
cms1.Show(dgvStandardTestD
Else
cms1.Hide()
End If
End If
End If
End If
Catch ex As Exception
strErr = gfrmID & "/dgvStandardTestData_Mous
MessageBox.Show(strErr, "User Notification", MessageBoxButtons.OK)
End Try
End Sub
Here is a sample...
Open in new window
Hope it helps
Yiannis