How to create GridView Update/Edit/Delete and in Asp.net C#

GridView.aspx


 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="GridView.WebForm1" %>


<!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="gridservice" runat="server" AutoGenerateColumns="False" OnRowCancelingEdit="gridservice_RowCancelingEdit" OnRowEditing="gridservice_RowEditing" OnRowUpdating="gridservice_RowUpdating" OnRowDeleting="gridservice_RowDeleting" AllowPaging="True" BackColor="LightGoldenrodYellow" BorderColor="Tan" BorderWidth="1px" CellPadding="2" ForeColor="Black" GridLines="None" 

                DataKeyNames="Id" OnSelectedIndexChanged="gridservice_SelectedIndexChanged" EmptyDataText="No Records has been added.">

                

                <AlternatingRowStyle BackColor="PaleGoldenrod" />

                <Columns>

                    <asp:BoundField DataField="Id" HeaderText="Id" InsertVisible="False" ReadOnly="True" SortExpression="Id" />

                    <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />

                    <asp:BoundField DataField="Password" HeaderText="Password" SortExpression="Password" />

                    <asp:CommandField ShowEditButton="true" HeaderText="Edit" />

                    <asp:CommandField ShowDeleteButton="true" HeaderText="Delete" />

                </Columns>

                <FooterStyle BackColor="Tan" />

                <HeaderStyle BackColor="Tan" Font-Bold="True" />

                <PagerStyle BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue" HorizontalAlign="Center" />

                <SelectedRowStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" />

                <SortedAscendingCellStyle BackColor="#FAFAE7" />

                <SortedAscendingHeaderStyle BackColor="#DAC09E" />

                <SortedDescendingCellStyle BackColor="#E1DB9C" />

                <SortedDescendingHeaderStyle BackColor="#C2A47B" />

                

            </asp:GridView>

            

        </div>

        <div>

            <asp:Label ID="lblresult" runat="server"></asp:Label>

        </div>

    </form>

</body>

</html>

--------------------------------------------------------------------------------------------------------------------------

GridView.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;
using System.Data.SqlClient;
using System.Configuration;

namespace GridView
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        string DBConnection = ConfigurationManager.ConnectionStrings["DBCS_connect"].ConnectionString;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                Bind();
            }
        }

        public void Bind()
        {
            SqlConnection con = new SqlConnection(DBConnection);
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandText = "Select * from tbllogin";
            cmd.CommandType = CommandType.Text;
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            gridservice.DataSource = ds;
            gridservice.DataBind();
        }

        protected void gridservice_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void gridservice_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            SqlConnection con = new SqlConnection(DBConnection);
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandText = "Delete From tblLogin Where Id = '"+Convert.ToInt32(gridservice.DataKeys[e.RowIndex].Value.ToString())+"'";
            cmd.CommandType = CommandType.Text;
            
            GridViewRow row = (GridViewRow)gridservice.Rows[e.RowIndex];
            Label lblDeleteid = (Label)row.FindControl("lblresult");
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
            Bind();
        }

        protected void gridservice_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gridservice.EditIndex = -1;
            Bind();
        }

        protected void gridservice_RowEditing(object sender, GridViewEditEventArgs e)
        {
            gridservice.EditIndex = e.NewEditIndex;
            Bind();
        }

        protected void gridservice_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            SqlConnection con = new SqlConnection(DBConnection);
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            int userID = Convert.ToInt32(gridservice.DataKeys[e.RowIndex].Value.ToString());
            GridViewRow row = (GridViewRow)gridservice.Rows[e.RowIndex];
            Label lblID = (Label)row.FindControl("lblresult");
            TextBox txtname = (TextBox)row.Cells[1].Controls[0];
            TextBox txtPassword = (TextBox)row.Cells[2].Controls[0];

            gridservice.EditIndex = -1;

            con.Open();
            cmd.CommandText = "Update tblLogin set name = '"+txtname.Text+"', password = '"+txtPassword.Text+"' where id = '"+userID+"'";
            cmd.ExecuteNonQuery();
            con.Close();
            Bind();

        }

    }
}
-------------------------------------------------------------------------------------------------------------------







0 Comments