How to edit or delete database record using grid view?

In this article i have explained about how to Update or Delete data base record using Grid view Operations and also I have explained about how to validate user input during Grid view edit operation. All the kinds of Grid view operation like Page index changing, grid view editing, grid view updating events etc. used in this application.

Grid view operation is easy to edit the data but i saw in the forum section some of the .Net Beginner struggle to Update or delete values using grid view operation. we update or delete record detail in the database using grid view data key values.

How to Update record using Grid view?
We have stored lots of record in the table and each record identified with help of primary key field. Here is the same logic applied we update each record based on the primary key value of that selected record. we have set primary key field of table as Gridview Data Key Name. After user click edit link and enter values click update button this time we update values of the selected record using Grid View Data Key value in the RowUpdating event.

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)

GridViewRow row = GridView1.Rows[e.RowIndex];
string eno;

eno = GridView1.DataKeys[e.RowIndex].Value.ToString();

TextBox empname = (TextBox)row.FindControl("txtempname");
TextBox sal = (TextBox)row.FindControl("txtsal");

sqlcmd = new SqlCommand("update emp set empname='" + empname.Text +"',sal='" + sal.Text +"' where eno='" + eno + "'" , sqlcon);
sqlcmd.CommandType = CommandType.Text;
GridView1.EditIndex = -1;

How to delete record using Grid view?
We can delete record in the same way of using Data Key values in the row deleting event.

protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
String eno;
eno = GridView1.DataKeys[e.RowIndex].Value.ToString();
sqlcmd = new SqlCommand("delete from emp where eno='" + eno +"'", sqlcon);
sqlcmd.CommandType = CommandType.Text;
GridData(); //Common method for refresh grid

How to validate user input during grid view edit operation?
In this example Grid view operations i used ASP.Net validation control for validate user input. This validation execute only user edit the record detail. I used Validation summary in the top of the grid view because error shows at the top position of the grid view instead of near textbox position.

How to use Grid view Page Index?
Grid view page index is nothing but it is used to split records several pages. For example if we have 100 records, this records is difficult to show at the same page because page length is long. Instead of that we can assign 10 records as per page, we mention client side of grid view PageSize="5", below of the grid view page numbers are available when ever user click the page number it refreshed records in the grid view using data bind operation.

GridView1.PageIndex = e.NewPageIndex;
// GridData() is a Common method for refresh grid check attached source code

Source Code Detail:
Here with i have attached entire source code. Download it and try to Edit or Delete record in the grid view operation.
Front End : ASP.NET
Code Behind : C#

Grid view operations is a easy way to update/delete operation. I hope this article is help to beginners for understand Grid view Operation.


  • Grid View Operations (42807-101142-Gridview_Operations.rar)
  • Comments

    Guest Author: shokofa25 Dec 2012

    thanks it was usefull

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: