Delete from datagridview by Right click

There are numerous approaches available to effectively execute a right-click action for selecting a specific row within a Datagridview and subsequently displaying a contextual menu to facilitate its deletion. In this particular instance, we are employing the Datagridview CellMouseUp event to successfully identify the desired row, and using the contextMenuStrip1_Click event within the vb.net programming language to seamlessly delete the selected row from the Datagridview.

CellMouseUp event

By utilizing the CellMouseUp event in the Datagridview, we can effortlessly capture the user's right-click action, enabling us to precisely detect the targeted row. This event serves as a fundamental mechanism that empowers us to accurately identify the row on which the user intends to perform an action.

contextMenuStrip1_Click event

Following the row selection process, the contextMenuStrip1_Click event takes center stage. This event, which is linked to the corresponding contextMenuStrip component, serves as the trigger point for initiating the deletion operation. Once the user interacts with the contextual menu by clicking the appropriate delete option, the associated event is invoked, effectively eliminating the selected row from the Datagridview.

delete from gridview by right click

Select row in a dataGridView by Right click

First you should drag a contextMenuStrip from your toolbox to your form. Then you create a contextMenuStrip item "Delete Row".

datagridview right click menu How can I select a row in datagridview when i press right clcik

When the right mouse button is pressed, the initial step entails locating the index of the selected row, followed by the subsequent display of the contextMenuStrip. This process is facilitated through the implementation of the CellMouseUp event within the dataGridView component. By utilizing this event, the row index can be accurately identified, enabling the appropriate menu item to be shown.

To achieve this, a global variable called "rowIndex" is employed. This variable serves the purpose of storing the value of the row index, which will be utilized later on to delete the corresponding row. By assigning the value of the row index to this global variable within the CellMouseUp event, it becomes readily accessible for deletion operations at a later stage.

If e.Button = MouseButtons.Right Then Me.DataGridView1.Rows(e.RowIndex).Selected = True Me.rowIndex = e.RowIndex Me.DataGridView1.CurrentCell = Me.DataGridView1.Rows(e.RowIndex).Cells(1) Me.ContextMenuStrip1.Show(Me.DataGridView1, e.Location) ContextMenuStrip1.Show(Cursor.Position) End If

We can delete the selected row in the contextMenuStrip1_Click event by using the global rowIdex value.

right click and delete Full Source VB.NET
Public Class Form1 Private rowIndex As Integer = 0 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim dt As New DataTable() dt.Columns.Add("Id", GetType(Integer)) dt.Columns.Add("Publisher Name", GetType(String)) dt.Columns.Add("Book", GetType(String)) For i As Integer = 1 To 10 dt.Rows.Add(i, "PubName" & i, "Book" & i) Next DataGridView1.DataSource = dt Me.DataGridView1.RowsDefaultCellStyle.BackColor = Color.Bisque Me.DataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.Beige End Sub Private Sub DataGridView1_CellMouseUp_1(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles DataGridView1.CellMouseUp If e.Button = MouseButtons.Right Then Me.DataGridView1.Rows(e.RowIndex).Selected = True Me.rowIndex = e.RowIndex Me.DataGridView1.CurrentCell = Me.DataGridView1.Rows(e.RowIndex).Cells(1) Me.ContextMenuStrip1.Show(Me.DataGridView1, e.Location) ContextMenuStrip1.Show(Cursor.Position) End If End Sub Private Sub ContextMenuStrip1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ContextMenuStrip1.Click If Not Me.DataGridView1.Rows(Me.rowIndex).IsNewRow Then Me.DataGridView1.Rows.RemoveAt(Me.rowIndex) End If End Sub End Class