The main purpose of "Dot Net Labs" provide the dot net program. You can learn dot net, .Net Core, C#, SQL, Linq step-by-step.

Monday 16 January 2017

How to add delete button on datagridview window form in asp.net


Here I will explain How to add delete button on dataGridView window form in asp.net. This is very important topic for dataGridView.

Firstly add contextMenuStrip from toolbox then write Delete Record in menu.
Second add CellMouseUp event in dataGridView and write the below code:

private void dataGridView1_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
        {
          
            if (e.Button == MouseButtons.Right)
            {
                delete_is =Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells["DSR_Code"].Value.ToString());
                this.contextMenuStrip1.Show(this.dataGridView1, e.Location);
                contextMenuStrip1.Show(Cursor.Position);
            }
        }

The output is


In last double click on Delete Record which is showing on contextMenuStrip


Write code on  deleteRecordToolStripMenuItem_Click

private void deleteRecordToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(cs);
            con.Open();
            SqlCommand com = new SqlCommand("delete from DSR_Master1 where DSR_Code=" + delete_is + "", con);
            com.ExecuteNonQuery();
            MessageBox.Show("Delete Successfully !");
            con.Close();
            datebind();
        }

0 comments:

Post a Comment

Do not enter spam link

Popular Posts