Thursday, 15 June 2017

Delete Data Grid View Row

Delete Data Grid View Row

There is lots of programming condition when we need to delete a row from data grid view. Deleting a record from Datagridview is pretty simple you just need know following point
  • Datagridview event for detect right click of mouse button
  • Context Menu setting

Datagridview event  (DataGridView1_CellMouseUp)

Private Sub DataGridView1_CellMouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles DataGridView1.CellMouseUp

        If e.Button = MouseButtons.Right Then
            DataGridView1.Rows(e.RowIndex).Selected = True
            ' rowIndex should me global variable
rowIndex = e.RowIndex       
            DataGridView1.CurrentCell = Me.DataGridView1.Rows(e.RowIndex).Cells(1)
            ContextMenuStrip1.Show(Me.DataGridView1, e.Location)
        End If
    End Sub

  • Add menu item in contect menu strip by items form its property
  • Set  DataGridView1 contextmenustrip property to Contextmenustrip1

Context menu code

Private Sub DeleteToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DeleteToolStripMenuItem.Click

    End Sub