GridView Edit, Update and Delete Button-All in one ASP.Net

Tutorial on GridView Edit, Update and Delete Button in ASP.Net. In case database not working, kindly create your own database and make the required changes in the connection string which is written in web.config file.
Download link of the following program has been given at the end of the post, you can use it for reference.

 

Default.aspx

 

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:GridView ID="grid" runat="server" 
        AutoGenerateColumns="false" AutoGenerateDeleteButton="true" 
        OnRowDeleting="grid_RowDeleting" AutoGenerateEditButton="true" 
        OnRowCancelingEdit="grid_RowCancelingEdit" OnRowEditing="grid_RowEditing" 
        OnRowUpdating="grid_RowUpdating">
        <Columns>
            <asp:TemplateField HeaderText="Roll No">
                <ItemTemplate>
                    <asp:Label ID="lblrl" runat="server" Text='<%#Eval("rollno") %>'>
                    </asp:Label>
                </ItemTemplate>
            </asp:TemplateField>

            <asp:TemplateField HeaderText="Name">
                <ItemTemplate>
                    <asp:Label ID="lblnm" runat="server" Text='<%#Eval("name") %>'>
                    </asp:Label>
                </ItemTemplate>

                <EditItemTemplate>
                    <asp:TextBox ID="txtnm" runat="server" Text='<%#Eval("name") %>'>

                    </asp:TextBox>
                </EditItemTemplate>
            </asp:TemplateField>

            <asp:TemplateField HeaderText="Course">
                <ItemTemplate>
                    <asp:Label ID="lblcs" runat="server" Text='<%#Eval("course") %>'>
                    </asp:Label>
                </ItemTemplate>

                <EditItemTemplate>
                    <asp:TextBox ID="txtcs" runat="server" Text='<%#Eval("course") %>'>

                    </asp:TextBox>
                </EditItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
    </div>
    </form>
</body>
</html>


Default.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;

public partial class _Default : System.Web.UI.Page
{
    SqlConnection con;
    SqlCommand cmd;
    SqlDataReader dr;
    string str = ConfigurationManager.ConnectionStrings["myconnectionstring"].ConnectionString;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack == false)
        {
            GridBind();
        }
    }

    private void GridBind()
    {
        //create connection
        con = new SqlConnection();
        con.ConnectionString = str;

        //create command
        cmd = new SqlCommand();
        cmd.CommandText = "Select * from student";

        //open connection
        cmd.Connection = con;
        con.Open();

        //execute command
        dr = cmd.ExecuteReader();
        if (dr.HasRows)
        {
            grid.DataSource = dr;
            grid.DataBind();
        }
        else
        {
            Response.Write("<script>alert('No Data');</script>");
        }

        //close connection
        con.Close();

    }
    protected void grid_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string rl = ((Label)grid.Rows[e.RowIndex].Cells[0].FindControl("lblrl")).Text;

        //create connection
        con = new SqlConnection();
        con.ConnectionString = str;

        //create command
        cmd = new SqlCommand();
        cmd.CommandText = "Delete from student where rollno=@RL";
        cmd.Parameters.AddWithValue("@RL", rl);

        //open connection
        cmd.Connection = con;
        con.Open();

        //execute command
        int rowcount = cmd.ExecuteNonQuery();
        if (rowcount > 0)
        {
            GridBind();
        }
        else {
            Response.Write("<script>alert('Error!');</script>");
        }
        
        //close connection
        con.Close();
    }
    protected void grid_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        string rl = ((Label)grid.Rows[e.RowIndex].Cells[0].FindControl("lblrl")).Text;
        string nm = ((TextBox)grid.Rows[e.RowIndex].Cells[0].FindControl("txtnm")).Text;
        string cs = ((TextBox)grid.Rows[e.RowIndex].Cells[0].FindControl("txtcs")).Text;

        //create connection
        con = new SqlConnection();
        con.ConnectionString = str;

        //create command
        cmd = new SqlCommand();
        cmd.CommandText = "Update student set name=@NM, course=@CS where rollno=@RL";
        cmd.Parameters.AddWithValue("@RL", rl);
        cmd.Parameters.AddWithValue("@NM", nm);
        cmd.Parameters.AddWithValue("@CS", cs);

        //open connection
        cmd.Connection = con;
        con.Open();

        //execute command
        int rowcount = cmd.ExecuteNonQuery();
        if (rowcount > 0)
        {
            grid.EditIndex = -1;
            GridBind();
        }
        else
        {
            Response.Write("<script>alert('Error');</script>");
        }

        //close connection
        con.Close();

    }
    protected void grid_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        grid.EditIndex = -1;
        GridBind();
    }
    protected void grid_RowEditing(object sender, GridViewEditEventArgs e)
    {
        grid.EditIndex = e.NewEditIndex;
        GridBind();
    }
}


Download Demo Program Here


JavaScript, ASP.Net & PHP Web Developer. Connect with me on Facebook and Twitter.

Share This Post

Related Articles

Powered by Paras Babbar · Designed by Paras Babbar