Edit and Update Record in GridView in ASP.Net

This example shows how to edit and update records in a GridView in ASP.NET. This example is helpful in situations where an administrator needs to edit several records from the database. For demonstration I have created a database (named Database.mdf) in which we have a table named tbl_Employee.
Table Schema used in this example
  1. CREATE TABLE [dbo].[tbl_Employee] (  
  2.     [ID]   INT          NOT NULL,  
  3.     [NameVARCHAR (50) NULL,  
  4.     [City] VARCHAR (50) NULL,  
  5.     PRIMARY KEY CLUSTERED ([ID] ASC)  
  6. );  
Let's Begin
  1. Drop a GridView Control from the toolbox and set AutoGenerateColumns to false.
  2. Add the Columns Collection (tag) that manages the collection of column fields.
  3. Add TemplateField inside the Columns Collection that is used to display custom content in a data-bound control.
  4. Add an ItemTemplate in the TemplateField that specifies the content to display for the items in a TemplateField.
  5. Add an EditItemTemplate in the TemplateField that specifies a custom user interface (UI) for the item in edit mode.
  6. Set the Command name property to Edit in the Edit button, Update in the Update button and Cancel in the Cancel Button depending on their respective Events.
  7. Add OnRowEditing, OnRowUpdating and OnRowCancelingEdit events to the GridView.
Default.aspx Code:
  1. <form id="form1" runat="server">  
  2.     <div>      
  3.         <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="6" OnRowCancelingEdit="GridView1_RowCancelingEdit"   
  4.   
  5. OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating">  
  6.             <Columns>  
  7.                 <asp:TemplateField>  
  8.                     <ItemTemplate>  
  9.                         <asp:Button ID="btn_Edit" runat="server" Text="Edit" CommandName="Edit" />  
  10.                     </ItemTemplate>  
  11.                     <EditItemTemplate>  
  12.                         <asp:Button ID="btn_Update" runat="server" Text="Update" CommandName="Update"/>  
  13.                         <asp:Button ID="btn_Cancel" runat="server" Text="Cancel" CommandName="Cancel"/>  
  14.                     </EditItemTemplate>  
  15.                 </asp:TemplateField>  
  16.                 <asp:TemplateField HeaderText="ID">  
  17.                     <ItemTemplate>  
  18.                         <asp:Label ID="lbl_ID" runat="server" Text='<%#Eval("ID") %>'></asp:Label>  
  19.                     </ItemTemplate>  
  20.                 </asp:TemplateField>  
  21.                 <asp:TemplateField HeaderText="Name">  
  22.                     <ItemTemplate>  
  23.                         <asp:Label ID="lbl_Name" runat="server" Text='<%#Eval("Name") %>'></asp:Label>  
  24.                     </ItemTemplate>  
  25.                     <EditItemTemplate>  
  26.                         <asp:TextBox ID="txt_Name" runat="server" Text='<%#Eval("Name") %>'></asp:TextBox>  
  27.                     </EditItemTemplate>  
  28.                 </asp:TemplateField>  
  29.                 <asp:TemplateField HeaderText="City">  
  30.                     <ItemTemplate>  
  31.                         <asp:Label ID="lbl_City" runat="server" Text='<%#Eval("City") %>'></asp:Label>  
  32.                     </ItemTemplate>  
  33.                     <EditItemTemplate>  
  34.                         <asp:TextBox ID="txt_City" runat="server" Text='<%#Eval("City") %>'></asp:TextBox>  
  35.                     </EditItemTemplate>  
  36.                 </asp:TemplateField>  
  37.             </Columns>  
  38.             <HeaderStyle BackColor="#663300" ForeColor="#ffffff"/>  
  39.             <RowStyle BackColor="#e7ceb6"/>  
  40.         </asp:GridView>  
  41.       
  42.     </div>  
  43. </form>  
Default.aspx.cs Code 
  1. using System;  
  2. using System.Data;  
  3. using System.Data.SqlClient;  
  4. using System.Configuration;  
  5. using System.Web.UI.WebControls;  
  6.   
  7. public partial class _Default : System.Web.UI.Page  
  8. {  
  9.     //Connection String from web.config File  
  10.     string cs = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;  
  11.     SqlConnection con;  
  12.     SqlDataAdapter adapt;  
  13.     DataTable dt;  
  14.     protected void Page_Load(object sender, EventArgs e)  
  15.     {  
  16.         if(!IsPostBack)  
  17.         {  
  18.             ShowData();  
  19.         }  
  20.     }  
  21.     //ShowData method for Displaying Data in Gridview  
  22.     protected void ShowData()  
  23.     {  
  24.         dt = new DataTable();  
  25.         con = new SqlConnection(cs);  
  26.         con.Open();  
  27.         adapt = new SqlDataAdapter("Select ID,Name,City from tbl_Employee",con);  
  28.         adapt.Fill(dt);  
  29.         if(dt.Rows.Count>0)  
  30.         {  
  31.             GridView1.DataSource = dt;  
  32.             GridView1.DataBind();  
  33.         }  
  34.         con.Close();  
  35.     }  
  36.   
  37.     protected void GridView1_RowEditing(object sender, System.Web.UI.WebControls.GridViewEditEventArgs e)  
  38.     {  
  39.         //NewEditIndex property used to determine the index of the row being edited.  
  40.         GridView1.EditIndex = e.NewEditIndex;  
  41.         ShowData();  
  42.     }  
  43.     protected void GridView1_RowUpdating(object sender, System.Web.UI.WebControls.GridViewUpdateEventArgs e)  
  44.     {  
  45.         //Finding the controls from Gridview for the row which is going to update  
  46.         Label id=GridView1.Rows[e.RowIndex].FindControl("lbl_ID"as Label;  
  47.         TextBox name = GridView1.Rows[e.RowIndex].FindControl("txt_Name"as TextBox;  
  48.         TextBox city = GridView1.Rows[e.RowIndex].FindControl("txt_City"as TextBox;  
  49.         con = new SqlConnection(cs);  
  50.         con.Open();  
  51.         //updating the record  
  52.         SqlCommand cmd = new SqlCommand("Update tbl_Employee set Name='"+name.Text+"',City='"+city.Text+"' where ID="+Convert.ToInt32(id.Text),con);  
  53.         cmd.ExecuteNonQuery();  
  54.         con.Close();  
  55.         //Setting the EditIndex property to -1 to cancel the Edit mode in Gridview  
  56.         GridView1.EditIndex = -1;  
  57.         //Call ShowData method for displaying updated data  
  58.         ShowData();  
  59.     }  
  60.     protected void GridView1_RowCancelingEdit(object sender, System.Web.UI.WebControls.GridViewCancelEditEventArgs e)  
  61.     {  
  62.         //Setting the EditIndex property to -1 to cancel the Edit mode in Gridview  
  63.         GridView1.EditIndex = -1;  
  64.         ShowData();  
  65.     }  
  66. }  
Final Preview: 
 
I hope you like it. Thanks.