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.
- 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 datagrid will fill as in the following screenshot:
Now for showing 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:
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.
- 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.
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!