VB.NET DataGridView - ContextMenu - Right Click To Delete

| Page Views: 1989

we see how to work with datagridview component and ContextMenu.We have data in our datagridview.When  we right click,the row gets selected and a contextmenu with delete option is shown.When we select delete,the items gets deleted from datagridview.

Public Class Form1
    Dim index As Integer = 0

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        'CONSTRUCT DATAGRIDVIEW
        DataGridView1.ColumnCount = 3
        DataGridView1.Columns(0).Name = "Name"
        DataGridView1.Columns(1).Name = "Position"
        DataGridView1.Columns(2).Name = "Team"

    End Sub

    'POPULATE DGVIEW WITH DATA
    Private Sub Populate()
        Dim row As String() = New String() {"Remy", "Striker", "Chelsea"}
        DataGridView1.Rows.Add(row)

        row = New String() {"Teryy", "Defender", "Chelsea"}
        DataGridView1.Rows.Add(row)
        row = New String() {"Walcott", "Winger", "Arsenal"}
        DataGridView1.Rows.Add(row)
        row = New String() {"Kompany", "Defender", "Man City"}
        DataGridView1.Rows.Add(row)
        row = New String() {"Jones", "Defender", "Man Utd"}
        DataGridView1.Rows.Add(row)
    End Sub

    Private Sub DataGridView1_CellMouseUp(sender As Object, e As DataGridViewCellMouseEventArgs) Handles DataGridView1.CellMouseUp
        'RIGHT CLICKS
        If e.Button = Windows.Forms.MouseButtons.Right Then

            DataGridView1.Rows(e.RowIndex).Selected = True
            index = e.RowIndex
            DataGridView1.CurrentCell = DataGridView1.Rows(e.RowIndex).Cells(1)
            ContextMenuStrip1.Show(DataGridView1, e.Location)
            ContextMenuStrip1.Show(Cursor.Position)
        End If

    End Sub

    Private Sub ContextMenuStrip1_Click(sender As Object, e As EventArgs) Handles ContextMenuStrip1.Click
        'CHECK IF NEW ROW
        If Not DataGridView1.Rows(index).IsNewRow Then
            DataGridView1.Rows.RemoveAt(index)
        End If
    End Sub

    Private Sub populateBtn_Click(sender As Object, e As EventArgs) Handles populateBtn.Click
        Populate()
    End Sub

    Private Sub clearBtn_Click(sender As Object, e As EventArgs) Handles clearBtn.Click
        DataGridView1.Rows.Clear()
    End Sub
End Class

Best Regards.

How do You Feel after reading this?

According to scientists, we humans have 8 primary innate emotions: joy, acceptance, fear, surprise, sadness, disgust, anger, and anticipation. Feel free to tell us how you feel about this article using these emotes or via the comment section.

About Me.

After completing his Software Engineering bachelors program, Oclemy(Clement Ochieng) these days is a man of two lives. At day he works for a startup in Nairobi, Kenya. At night he works tirelessly on building ProgrammingWizards TV, a tv channel for student coders and this website to help share the source code. In between he practices Meditation and Self actualization to help him keep balance. He also likes going for long solo walks to connect more with nature.




Recommendations


What do You Think


Previous Post Next Post