GridView With Edit,Update,Delete and Pagination

Introduction
 
In this blog I will explain how to Edit, Update, Delete with pagination in asp.net gridview with c#.
 
Step 1
 
First of all create database in sql server like this-  
  1. --create databse grid  
  2. --use grid  
  3. create table gridview  
  4. (  
  5. sid int,  
  6. name varchar(100),  
  7. address varchar(200)  
  8. );  
  9.   
  10. insert into gridview values(1,'Mukesh Kumar','New Delhi')  
  11. insert into gridview values(2,'Deepa kumar','Utra Khand')  
  12. insert into gridview values(3,'Pankaj Kumar','New Delhi')  
  13. insert into gridview values(4,'Sanjeev Kumar','New Delhi')  
  14. insert into gridview values(5,'Pawan Kumar','New Delhi')  
Step 2
 
Create gridview layout in aspx file like this
 
 
 
 
And the above code is given bellow 
  1. <asp:GridView ID="grid1" runat="server" AutoGenerateColumns="False" CssClass="grid"  
  2.     OnRowEditing="grid1_RowEditing"  
  3.     AllowPaging="True" PageSize="4"  
  4.     OnPageIndexChanging="grid1_PageIndexChanging" BackColor="White"  
  5.     BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px" CellPadding="5"  
  6.     OnRowCancelingEdit="grid1_RowCancelingEdit">  
  7.     <PagerSettings FirstPageText="First" LastPageText="Last" NextPageText="Next" PreviousPageText="Previous" />  
  8.     <RowStyle BackColor="White" ForeColor="#330099" />  
  9.     <Columns>  
  10.         <asp:TemplateField HeaderText="Serial No">  
  11.             <ItemTemplate>  
  12.                 <asp:Label ID="lblsid" runat="server" Text='<%# Eval("sid")%>'></asp:Label>  
  13.             </ItemTemplate>  
  14.         </asp:TemplateField>  
  15.         <asp:TemplateField HeaderText="Name">  
  16.             <ItemTemplate>  
  17.                 <asp:Label ID="lblName" runat="server" Text='<%#Eval("name") %>'></asp:Label>  
  18.             </ItemTemplate>  
  19.             <EditItemTemplate>  
  20.                 <asp:TextBox ID="txtName" runat="server" Text='<%#Eval("name") %>'></asp:TextBox>  
  21.             </EditItemTemplate>  
  22.         </asp:TemplateField>  
  23.         <asp:TemplateField HeaderText="Address">  
  24.             <ItemTemplate>  
  25.                 <asp:Label ID="lblAddress" runat="server" Text='<%#Eval("address") %>'></asp:Label>  
  26.             </ItemTemplate>  
  27.             <EditItemTemplate>  
  28.                 <asp:TextBox ID="txtAddress" runat="server" Text='<%#Bind("address") %>'></asp:TextBox>  
  29.             </EditItemTemplate>  
  30.         </asp:TemplateField>  
  31.   
  32.         <asp:CommandField ShowEditButton="True" HeaderText="Edit" />  
  33.         <asp:TemplateField HeaderText="Delete1">  
  34.             <ItemTemplate>  
  35.                 <asp:ImageButton ID="imgDelete" runat="server" ImageUrl="~/delete.png"  
  36.                     OnClick="imgDelete_Click" Style="height: 15px" />  
  37.             </ItemTemplate>  
  38.         </asp:TemplateField>  
  39.     </Columns>  
  40.     <FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />  
  41.     <PagerStyle BorderStyle="None" HorizontalAlign="Center" />  
  42.     <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />  
  43.     <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />  
  44. </asp:GridView>  
Now come in code behine .cs file
 
  1. using System;  
  2. using System.Configuration;  
  3. using System.Data;  
  4. using System.Linq;  
  5. using System.Web;  
  6. using System.Web.Security;  
  7. using System.Web.UI;  
  8. using System.Web.UI.HtmlControls;  
  9. using System.Web.UI.WebControls;  
  10. using System.Web.UI.WebControls.WebParts;  
  11. using System.Xml.Linq;  
  12. using System.Data.SqlClient;  
  13. public partial class _Default : System.Web.UI.Page  
  14. {  
  15.     protected void Page_Load(object sender, EventArgs e)  
  16.     {  
  17.         if (!IsPostBack)  
  18.         {  
  19.             BindGrid();  
  20.         }  
  21.     }  
  22.     protected void grid1_RowEditing(object sender, GridViewEditEventArgs e)  
  23.     {  
  24.         grid1.EditIndex = e.NewEditIndex;  
  25.         BindGrid();  
  26.     }  
  27.     protected void grid1_RowUpdating(object sender, GridViewUpdateEventArgs e)  
  28.     {  
  29.         GridViewRow ro = (GridViewRow)grid1.Rows[e.RowIndex];  
  30.         Label id = (Label)ro.FindControl("lblsid");  
  31.         TextBox name = (TextBox)ro.FindControl("txtName");  
  32.         TextBox address = (TextBox)ro.FindControl("txtAddress");  
  33.     }  
  34.     public void BindGrid()  
  35.     {  
  36.         SqlConnection con = new SqlConnection("Data Source=HP-PC;Initial Catalog=grid;User ID=sa;pwd=123");  
  37.         SqlCommand cmd = new SqlCommand("select * from gridview", con);  
  38.         SqlDataAdapter da = new SqlDataAdapter(cmd);  
  39.         DataSet ds = new DataSet();  
  40.         da.Fill(ds);  
  41.         grid1.DataSource = ds;  
  42.         grid1.DataBind();  
  43.     }  
  44.     protected void grid1_PageIndexChanging(object sender, GridViewPageEventArgs e)  
  45.     {  
  46.         grid1.PageIndex = e.NewPageIndex;  
  47.         BindGrid();  
  48.     }  
  49.     protected void grid1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)  
  50.     {  
  51.         grid1.EditIndex = -1;  
  52.         BindGrid();  
  53.     }  
  54.     protected void imgDelete_Click(object sender, ImageClickEventArgs e)  
  55.     {  
  56.         int i = 0;  
  57.         ImageButton imgdetails = sender as ImageButton;  
  58.         GridViewRow gvrow = (GridViewRow)imgdetails.NamingContainer;  
  59.         Label id = (Label)gvrow.FindControl("lblsid");  
  60.         SqlConnection con = new SqlConnection("Data Source=HP-PC;Initial Catalog=grid;User ID=sa;pwd=123");  
  61.         SqlCommand cmd = new SqlCommand("delete from  gridview where sid=@sid", con);  
  62.         cmd.Parameters.AddWithValue("@sid", id.Text);  
  63.         con.Open();  
  64.         i = cmd.ExecuteNonQuery();  
  65.         con.Close();  
  66.         if (i > 0)  
  67.         {  
  68.             Response.Write("record deleted");  
  69.         }  
  70.     }  
  71. }