Delete row from datagridview by right click

Numerous solutions exist to enable the selection of a row in a DataGridView through right-clicking, subsequently displaying a contextual menu to facilitate deletion. In this particular implementation, we use the CellMouseUp event to handle row selection, while the contextMenuStrip1_Click event is responsible for removing the selected row from the DataGridView.

Right click to select row in dataGridView

Right click to select row in dataGridView

To begin, it is essential to add a contextMenuStrip control from the toolbox to the designated form. Once added, create a menu item named "Delete Row" within the contextMenuStrip, which will serve as the trigger for row deletion.

Right click contextMenuStrip

How can I select a row in datagridview on right clcik

Moving forward, the subsequent step involves identifying the appropriate row index upon right-click and displaying the contextMenuStrip accordingly. In this implementation, we utilize the CellMouseUp event of the dataGridView to capture the row index and exhibit the respective menu item. To facilitate future row deletion, a global variable named "rowIndex" is employed to retain the row index value.

if (e.Button == MouseButtons.Right) { this.dataGridView1.Rows[e.RowIndex].Selected = true; this.rowIndex = e.RowIndex; this.dataGridView1.CurrentCell = this.dataGridView1.Rows[e.RowIndex].Cells[1]; this.contextMenuStrip1.Show(this.dataGridView1, e.Location); contextMenuStrip1.Show(Cursor.Position); }

Right click and delete row from datagridview

After retrieveing the row index, next step is to delete the specified row. In the contextMenuStrip1_Click event delete the row using rowindex value.

delete row Full Source C#
using System; using System.Data; using System.Drawing; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { private int rowIndex = 0; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { DataTable dt = new DataTable(); dt.Columns.Add("Id", typeof(int)); dt.Columns.Add("Publisher Name", typeof(string)); dt.Columns.Add("Book", typeof(string)); for (int i = 1; i < 11; i++) { dt.Rows.Add(i, "PubName" + i, "Book"+i); } dataGridView1.DataSource = dt; this.dataGridView1.RowsDefaultCellStyle.BackColor = Color.Bisque; this.dataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.Beige; } private void dataGridView1_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e) { if (e.Button == MouseButtons.Right) { this.dataGridView1.Rows[e.RowIndex].Selected = true; this.rowIndex = e.RowIndex; this.dataGridView1.CurrentCell = this.dataGridView1.Rows[e.RowIndex].Cells[1]; this.contextMenuStrip1.Show(this.dataGridView1, e.Location); contextMenuStrip1.Show(Cursor.Position); } } private void contextMenuStrip1_Click(object sender, EventArgs e) { if (!this.dataGridView1.Rows[this.rowIndex].IsNewRow) { this.dataGridView1.Rows.RemoveAt(this.rowIndex); } } } }

Conclusion

Following this approach, users can seamlessly select rows in the DataGridView through right-clicking, and subsequently trigger the contextMenuStrip to initiate row deletion. This implementation ensures a streamlined and intuitive user experience within the DataGridView.