UPDATE and DELETE from DataGridView Control
How to Update & Delete a selected row from DataGridView Control ?
1 Answer
Know the answer? Post it. Somebody with the same question will find it here.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Ramjee9 reputationPosted Apr 3, 2011, 10:13 PM EDT
You just gothrough the below update and delete codings in C#. It will help you to get an idea in VB too.
Update:
mycon.Open();
GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
Label lbl = (Label)row.FindControl("lblid");
TextBox textdesignation = (TextBox)row.FindControl("textbox1");
SqlCommand cmd = new SqlCommand("update emp set designation='" +textdesignation.Text + "' where empid=" + lbl.Text + "", mycon);
GridView1.EditIndex = -1;
cmd.ExecuteNonQuery();
mycon.Close();
After the line mycon.close you just write the codings to view the gridview data. It will show the updated and also the whole data's on the grid. Here i just updated only one column(designation). you just try more than one in your project.
Delete:
mycon.Open();
GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
Label lbldel = (Label)row.FindControl("lblid");
SqlCommand cmd = new SqlCommand("delete from emp where empid=" + lbldel.Text + "", mycon);
cmd.ExecuteNonQuery();
mycon.Close();
After the line mycon.close you just write the codings to view the gridview data. It will show the remaining data's on the grid.