Real Time Use Of Context Menu Strip In Windows Form Application

Here I have explained how to use the context menu strip to perform some operations in Windows form. If you don't know about context menu strips then I will strongly suggest you check my tutorial about ContextMenuStrip. I have clearly explained how to use ContextMenuStrip in Windows form.

Context MenuStrip in Windows form

Now here, I have explained how to delete a record from DataGridView using ContextMenuStrip.

Before that create a Windows project and add a DataGrid to show data and a ContextMenuStrip on it as in the following screenshot:

DataGrid

Now Add a Menu as "Delete" in the menu.

Delete

Now write the following code to fill the data view.

public partial class Form1 : Form
{
    SqlConnection con = new SqlConnection("Data Source=DEBENDRA;Initial Catalog=Students;User ID=sa;Password=123");
    int Index = 0; // Declaring a variable
    SqlDataAdapter da;
    public Form1()
    {
        InitializeComponent();
    }
    private void Form1_Load(object sender, EventArgs e)
    {
        filldata(); // Calling the method to fill the datagrid
    }
    public void filldata()
    {
        da = new SqlDataAdapter("select * from tbl_studentdetails", con);
        DataTable dt = new DataTable();
        da.Fill(dt);
        dataGridView1.DataSource = dt;
    }
}

Now the data will fill as in the following screenshot.

Student Details

Now to show the context menu on right-click, write the following code.

private void dataGridView1_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e) 
{   
    if (e.Button == MouseButtons.Right) 
    {   
        this.dataGridView1.Rows[e.RowIndex].Selected = true;   
        this.Index = e.RowIndex;   
        this.dataGridView1.CurrentCell = this.dataGridView1.Rows[e.RowIndex].Cells[1];   
        this.contextMenuStrip1.Show(this.dataGridView1, e.Location);   
        contextMenuStrip1.Show(Cursor.Position);   
    }   
}

Now set the Selection Mode property of datagrid to FullRowSelect as in the following screenshot.

 Mode property

As FullRow is selected when a cell is selected then a full row of data grid will automatically be selected.

Now for deleting the record double click on the "Delete" menu and write the following code in it.

private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
{
    DialogResult dr = MessageBox.Show("Are you sure want to Delete", "confirmation", MessageBoxButtons.YesNo);
    
    if (dr == DialogResult.Yes)
    {
        string studentId = dataGridView1.Rows[Index].Cells[1].Value.ToString(); // Getting the ID by index of row.
        
        SqlCommand cmd = new SqlCommand("delete from tbl_studentdetails where RollNo='" + studentId + "'", con); 
        con.Open();
        int result = cmd.ExecuteNonQuery();  
        if (result == 1)
        {
            MessageBox.Show("Record Deleted Successfully");
            filldata();
        }
        else
        {
            MessageBox.Show("Record not Deleted....Please try again.");
        }
    }
}

So when we delete the record it will show like this.

Record

When we right-click on any row first the whole row is selected and the "Delete " menu will come up.

When you click on Delete then it will ask for confirmation.

Confirmation

When you click yes then.

Yes

Now if you look at the table, the record was deleted successfully.

Record

So in this way, we can implement a simple delete option by ContextMenuStrip.Thus you can perform any operation using ContextMenuStrip.

Thank you!