Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27470

VS 2010 Datagridview and ContextMenuStrip

$
0
0
Hi!

On my Form there is a DataGridView that has a ContextMenuStrip bound to.
ContextMenuStrip has one button "Delete".

When I Right click on DataGridView then it displays the ContextMenuStrip, highlights entire row BUT the little arrow on the rowheader doesn't come along. That means if I click "Delete" button it will delete the row where this arrow is positioned.

Is there a way to get around this problem?

Here is my code for deleting:
Code:

Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click

        Try
            If Not DataGridView.CurrentRow.IsNewRow Then
                DataGridView.Rows.Remove(DataGridView.CurrentRow)
                Me.Validate()
                Me.BindingSource.EndEdit()
                Me.TableAdapterManager.UpdateAll(Me.DataSet)
                MsgBox("Row deleted successfully!", MsgBoxStyle.Information, "Database")
            End If
        Catch ex As Exception
            MsgBox("Row was not deleted (row not selected)", MsgBoxStyle.Information, "Database")
        End Try
    End Sub


And here is my DataGridView right click code:
Code:

Private Sub DataGridView_CellMouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles DataGridView.CellMouseDown


        If e.Button = Windows.Forms.MouseButtons.Right AndAlso e.RowIndex >= 0 AndAlso e.ColumnIndex >= 0 Then
            DataGridView.ClearSelection()
            DataGridView.Rows.Item(e.RowIndex).Selected = True
        End If

    End Sub

P.S!
Have tried with event cellmouseclick too...doesn't work.

Viewing all articles
Browse latest Browse all 27470

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>