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-

 create databse grid
use grid
create table gridview
(
sid int,
name varchar(100),
address varchar(200)
)
insert into gridview values(1,'Mukesh Kumar','New Delhi')
insert into gridview values(2,'Deepa kumar','Utra Khand')
insert into gridview values(3,'Pankaj Kumar','New Delhi')
insert into gridview values(4,'Sanjeev Kumar','New Delhi')
insert into gridview values(5,'Pawan Kumar','New Delhi')

Step 2

Create gridview layout in aspx file like this
grid-1.jpg

grid-2.jpg
 
And the layout code is given below

  <asp:GridView ID="grid1" runat="server" AutoGenerateColumns="False"  CssClass="grid"  
        onrowediting="grid1_RowEditing" 
         AllowPaging="True" PageSize="4" 
        onpageindexchanging="grid1_PageIndexChanging" BackColor="White" 
        BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px" CellPadding="5" 
        onrowcancelingedit="grid1_RowCancelingEdit">
        <PagerSettings FirstPageText="First" LastPageText="Last"  NextPageText="Next" PreviousPageText="Previous"  />
        <RowStyle BackColor="White" ForeColor="#330099"  />
    <Columns>
    <asp:TemplateField HeaderText="Serial No">    
    <ItemTemplate>
    <asp:Label ID="lblsid" runat="server" Text='<%# Eval("sid")%>'></asp:Label>
    </ItemTemplate>
    </asp:TemplateField>    
    <asp:TemplateField HeaderText="Name">
    <ItemTemplate>
    <asp:Label ID="lblName" runat="server" Text='<%#Eval("name") %>'></asp:Label>
    </ItemTemplate>
    <EditItemTemplate>
    <asp:TextBox ID="txtName" runat="server" Text='<%#Eval("name") %>'></asp:TextBox>
    </EditItemTemplate>
    </asp:TemplateField>    
    <asp:TemplateField HeaderText="Address">        
    <ItemTemplate>
    <asp:Label ID="lblAddress" runat="server" Text='<%#Eval("address") %>'></asp:Label>
    </ItemTemplate>
    <EditItemTemplate>    
    <asp:TextBox ID="txtAddress" runat="server" Text='<%#Bind("address") %>'></asp:TextBox>
    </EditItemTemplate>    
    </asp:TemplateField>
    
     <asp:CommandField ShowEditButton="True" HeaderText="Edit" />
     <asp:TemplateField HeaderText="Delete1">
     <ItemTemplate>
     <asp:ImageButton ID="imgDelete" runat="server" ImageUrl="~/delete.png" 
             onclick="imgDelete_Click" style="height: 15px" />
     </ItemTemplate>    
     </asp:TemplateField>
    </Columns>
        <FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
        <PagerStyle BorderStyle="None" HorizontalAlign="Center" />
        <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />
        <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />
    </asp:GridView>

Step 3

Now come in Code Behind .cs file

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {       
        if (!IsPostBack)
        {
            BindGrid();
        }
    }
    protected void grid1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        grid1.EditIndex = e.NewEditIndex;
        BindGrid();
    }    
    protected void grid1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        GridViewRow ro = (GridViewRow)grid1.Rows[e.RowIndex];
        Label id = (Label)ro.FindControl("lblsid");
        TextBox name = (TextBox)ro.FindControl("txtName");
        TextBox address=(TextBox)ro.FindControl("txtAddress");        
    }
    public void BindGrid()
    {
        SqlConnection con = new SqlConnection("Data Source=HP-PC;Initial Catalog=grid;User ID=sa;pwd=123");
        SqlCommand cmd = new SqlCommand("select * from gridview", con);
        SqlDataAdapter da=new SqlDataAdapter(cmd);
        DataSet ds=new DataSet();
        da.Fill(ds);
        grid1.DataSource = ds;
        grid1.DataBind();
    }
    protected void grid1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grid1.PageIndex = e.NewPageIndex;
        BindGrid();
    }
    protected void grid1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        grid1.EditIndex = -1;
        BindGrid();
    }
    protected void imgDelete_Click(object sender, ImageClickEventArgs e)
    {
        int i = 0;
        ImageButton imgdetails = sender as ImageButton;
        GridViewRow gvrow = (GridViewRow)imgdetails.NamingContainer;
        Label id = (Label)gvrow.FindControl("lblsid");
        SqlConnection con = new SqlConnection("Data Source=HP-PC;Initial Catalog=grid;User ID=sa;pwd=123");
        SqlCommand cmd = new SqlCommand("delete from  gridview where sid=@sid", con);
        cmd.Parameters.AddWithValue("@sid", id.Text);
        con.Open();
         i=cmd.ExecuteNonQuery();
        con.Close();
        if (i > 0)
        {
            Response.Write("record deleted");
        }
    }
}
gridview control layout
Ebook Download
View all
Learn
View all