Real Time Use Of Context Menu Strip In Windows Form Application

Context Menu Strip in Windows form Application

Here I have explained how to use context menu strip to perform some operation in windows form. If you don't know about context menu strip then I will strongly suggest you to check my tutorial about ContextMenuStrip. I have clearly explained how to use ContextMenuStrip 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:



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

 

Now write the following code to fill the datagrid view.
  1. public partial class Form1: Form {  
  2.     SqlConnection con = new SqlConnection("Data Source=DEBENDRA;Initial Catalog=Students;User ID=sa;Password=123");  
  3.     int Index = 0;\\  
  4.     Declaring a variable  
  5.     SqlDataAdapter da;  
  6.     public Form1() {  
  7.         InitializeComponent();  
  8.     }  
  9.     private void Form1_Load(object sender, EventArgs e) {  
  10.         filldata();\\  
  11.         calling the method to fill the datagrid.  
  12.     }  
  13.     public void filldata() {  
  14.         da = new SqlDataAdapter("select * from tbl_studentdetails", con);  
  15.         DataTable dt = new DataTable();  
  16.         da.Fill(dt);  
  17.         dataGridView1.DataSource = dt;  
  18.     }  
  19. }  
Now the datagrid will fill as in the following screenshot:

 

Now for showing the context menu on right click, write the following code. 
  1. private void dataGridView1_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e) {  
  2.     if (e.Button == MouseButtons.Right) {  
  3.         this.dataGridView1.Rows[e.RowIndex].Selected = true;  
  4.         this.Index = e.RowIndex;  
  5.         this.dataGridView1.CurrentCell = this.dataGridView1.Rows[e.RowIndex].Cells[1];  
  6.         this.contextMenuStrip1.Show(this.dataGridView1, e.Location);  
  7.         contextMenuStrip1.Show(Cursor.Position);  
  8.     }  
  9. }   
Now set the Selection Mode property of datagrid to FullRowSelect as in the following screenshot:

 

As FullRow is selected when a cell is selected then full row of datagrid will automatically select.

Now for deleting the record double click on "Delete" menu and write the following code in it. 
  1. private void deleteToolStripMenuItem_Click(object sender, EventArgs e) {  
  2.     DialogResult dr = MessageBox.Show("Are you sure want to Delete""confirmation", MessageBoxButtons.YesNo);  
  3.     if (dr == DialogResult.Yes) {  
  4.         string studentId = dataGridView1.Rows[Index].Cells[1].Value.ToString();\\  
  5.         getting the id by index of row.  
  6.         SqlCommand cmd = new SqlCommand("delete from tbl_studentdetails where RollNo='" + studentId + "'", con);  
  7.         con.Open();  
  8.         int result = cmd.ExecuteNonQuery();  
  9.         if (result == 1) {  
  10.             MessageBox.Show("Record Deleted Successfully");  
  11.             filldata();  
  12.         } else {  
  13.             MessageBox.Show("Record not Deleted....Please try again.");  
  14.         }  
  15.     }  
  16. }  
So when we delete the record it will show like this.

 

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

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

 

When you click yes then.

 

Now if you look at the table, the record deleted successfully:

 

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