UPDATE and DELETE from DataGridView Control
How to Update & Delete a selected row from DataGridView Control ?
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
RamjeePosted Apr 4, 2011, 2:13 AM
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.