Net-informations.com
SiteMap  | About    

Delete row from datagridview by right click

There are many solutions for right click to select a row in a Datagridview and show a menu to delete it. Here we are using CellMouseUp event to select row and contextMenuStrip1_Click event to delete the row from datagridview.

Right click to select row in dataGridView

Right click to select row in dataGridView

The first thing you will want to do is to drag a contextMenuStrip from your toolbox to your form. Then you create a menu item "Delete Row" in the contextMenuStrip.

Right click contextMenuStrip

How can I select a row in datagridview on right clcik

The next step is to find the right click row index and show the contextMenuStrip. Here we are using CellMouseUp of dataGridView to indentify the row index and show the menu item. A global variable rowIndex is assigned the row index value for later delete this row.

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



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);
            }
        }
    }
}




net-informations.com (C) 2016    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.