4
votes

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?

1

1 Answers

5
votes
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