Datagridview - Focus on Cell that was right clicked

25,381
Private Sub DataGridView1_MouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DataGridView1.MouseClick
    If e.Button = Windows.Forms.MouseButtons.Right Then
        rowClicked = DataGridView1.HitTest(e.Location.X, e.Location.Y).RowIndex
        ContextMenuStrip1.Items.Add(rowClicked.ToString)
        ContextMenuStrip1.Show(DataGridView1, e.Location)
        ContextMenuStrip1.Items.Clear()
    End If
End Sub

Edit: Updated to handle a context menu strip.

That should give you the row index of the row that was right clicked using the mouse coordinates. Which should let you delete the row based on knowing the index.

Edit

Per Your comment on it not working this is my code

I have a Solution with a WinForm with a dataGridView added to it. and this is the code in the form.

Public Class Form1

    Dim bindS As New BindingSource
    Dim rowClicked As Integer
    Private Sub DataGridView1_MouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DataGridView1.MouseClick
        If e.Button = Windows.Forms.MouseButtons.Right Then
            rowClicked = DataGridView1.HitTest(e.Location.X, e.Location.Y).RowIndex
            ContextMenuStrip1.Items.Add(rowClicked.ToString)
            ContextMenuStrip1.Show(DataGridView1, e.Location)
            ContextMenuStrip1.Items.Clear()
        End If

    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim s As New List(Of String)
        s.Add("String one")
        s.Add("String Two")
        bindS.DataSource = s
        DataGridView1.DataSource = bindS


    End Sub
End Class

Right Clicking on a row shows the correct row index

Make sure that the event args that you are handling are the System.Windows.Forms.MouseEventArgs I noticed that you're handling the cell click

Share:
25,381
ErocM
Author by

ErocM

Updated on June 26, 2020

Comments

  • ErocM
    ErocM almost 4 years

    I have a datagridview that I have put a ContextMenuStrip1 on. I would like it to remove a row in the datagridview when the row is right clicked on and they click on "delete row". I have the delete working and the menu is showing up but this isn't firing when you right click on the datagridview.

    This is where I am setting the row to edit:

       Private Sub ModifyRowToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ModifyRowToolStripMenuItem.Click
        If Not datagridview_TagAssignment.CurrentRow Is Nothing Then
          datagridview_TagAssignment.CurrentCell = datagridview_TagAssignment.Item(0, datagridview_TagAssignment.CurrentRow.Index)
          datagridview_TagAssignment.BeginEdit(True)
        End If
      End Sub
    

    I am always ending up on row(0) and never the row I right clicked on.

     Private Sub datagridview_TagAssignment_CellMouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles datagridview_TagAssignment.CellMouseClick
        If e.Button = Windows.Forms.MouseButtons.Right AndAlso e.RowIndex >= 0 Then
          datagridview_TagAssignment.Rows(e.RowIndex).Selected = True
        End If
      End Sub
    

    Anyone have any suggestions?

  • ErocM
    ErocM about 14 years
    I added this and put a debug on this line and it did not stop when I right clicked on the datagridview. That's really what my problem has been from the start. Do I not have something right setup for the right click to work on the mouseclick?
  • ErocM
    ErocM about 14 years
    Ok, I found out the reason why it's not catching the right click. If I remove the "ContextMenuStrip1" from the datagridview, the right click is now seen and your code works. Any suggestions how to get around this?
  • msarchet
    msarchet about 14 years
    yea, I edited the code so you can see how to show the context menu yet still get the row index. In the contextmenustrip I'm using I'm showing the items as the right clicked row. Now the <b>focused</b> row won't change when you right click, but the clicked row index is correct.