Asp.Net

Tuesday, 12 August 2014

GridView Edit,Delete.Update and Add New Row in ASP.NET

Aspx Page Code:

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<script type="text/javascript" language="javascript"></script>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="gvdetails" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None"
            OnRowEditing="OnRowEditing_gvdetails" ShowFooter="true" OnRowUpdating="OnRowUpdating_gvdetails"
            OnRowDeleting="OnRowDeleting_gvdetails" OnRowCancelingEdit="OnRowCancelingEdit_gvdetails"
            DataKeyNames="Id" AutoGenerateColumns="false" OnRowCommand="OnRowCommand_gvdetails">
            <Columns>
                <asp:TemplateField HeaderText="Name">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtname" runat="server" Text='<%#Eval("Name") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lblname" runat="server" Text='<%#Eval("Name") %>'></asp:Label>
                    </ItemTemplate>
                    <FooterTemplate>
                        <asp:TextBox ID="txtfname" runat="server"></asp:TextBox>
                    </FooterTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Id">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtid" runat="server" Text='<%#Eval("Id") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lblid" runat="server" Text='<%#Eval("Id") %>'></asp:Label>
                    </ItemTemplate>
                    <FooterTemplate>
                        <asp:TextBox ID="txtfid" runat="server"></asp:TextBox>
                    </FooterTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="City">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtcity" runat="server" Text='<%#Eval("City") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lblcity" runat="server" Text='<%#Eval("City") %>'></asp:Label>
                    </ItemTemplate>
                    <FooterTemplate>
                        <asp:TextBox ID="txtfcity" runat="server"></asp:TextBox>
                    </FooterTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Mob">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtmob" runat="server" Text='<%#Eval("Mob") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lblmob" runat="server" Text='<%#Eval("Mob") %>'></asp:Label>
                    </ItemTemplate>
                    <FooterTemplate>
                        <asp:TextBox ID="txtfmob" runat="server"></asp:TextBox>
                    </FooterTemplate>
                </asp:TemplateField>
                <asp:CommandField ShowEditButton="true" CausesValidation="false" ShowDeleteButton="true"
                    ButtonType="Button" EditText="Edit" />
                <asp:TemplateField>
                    <FooterTemplate>
                        <asp:Button ID="btnadd" runat="server" CommandName="Add" Text="Add" Width="60px" />
                    </FooterTemplate>
                </asp:TemplateField>
            </Columns>
            <RowStyle BackColor="#EFF3FB" />
            <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
            <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
            <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <EditRowStyle BackColor="#2461BF" />
            <AlternatingRowStyle BackColor="White" />
        </asp:GridView>
    </div>
    <asp:Button ID="btnsubmit" runat="server" Text="Submit" OnClick="btnsubmit_Click" OnClientClick="javascript: return confirm('Do you want to Save this invoice as PDF?')" />
    </form>
</body>
</html>

Code Behind File i e, .cs File

using System;
using System.Collections;
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;

namespace Practice
{
    public partial class grid : System.Web.UI.Page
    {
        //Connection to Database
        SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["con"].ToString());

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                gridvalues();
            }

        }
        //Gridview binding
        private void gridvalues()
        {
            DataTable dt = new DataTable();
            con.Open();
            string selquery = "select * from Tb_Employee";
            SqlCommand cmd = new SqlCommand(selquery,con);
            cmd.CommandType = CommandType.Text;
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(dt);
            gvdetails.DataSource = dt;
            gvdetails.DataBind();
            con.Close();
       }

        //on row Gridview Editing
        protected void OnRowEditing_gvdetails(object sender, GridViewEditEventArgs e)
        {
            gvdetails.EditIndex = e.NewEditIndex;
            gridvalues();
        }
        //on row Gridview Deleting
        protected void OnRowDeleting_gvdetails(object sender, GridViewDeleteEventArgs e)
        {
            int index = e.RowIndex;
            string id = gvdetails.DataKeys[index].Values["Id"].ToString();

            string selquery = "delete from Tb_Employee where Id='"+id+"'";
            con.Open();
            SqlCommand cmd = new SqlCommand(selquery, con);
            cmd.CommandType = CommandType.Text;
            cmd.ExecuteNonQuery();
            con.Close();
            gridvalues();
        }
        //on row Gridview Canceling Edit
        protected void OnRowCancelingEdit_gvdetails(object sender, GridViewCancelEditEventArgs e)
        {
            gvdetails.EditIndex = -1;
            gridvalues();
           
        }
        //on row Gridview Updating
        protected void OnRowUpdating_gvdetails(object sender, GridViewUpdateEventArgs e)
        {
          int index = e.RowIndex;
         
          string id = gvdetails.DataKeys[index].Values["Id"].ToString();
          TextBox txtname =((TextBox)gvdetails.Rows[e.RowIndex].FindControl("txtname"));
          TextBox txtcity = ((TextBox)gvdetails.Rows[e.RowIndex].FindControl("txtcity"));
          TextBox txtmob = ((TextBox)gvdetails.Rows[e.RowIndex].FindControl("txtmob"));

          string selquery = "update Tb_Employee set Name='" + txtname.Text + "',City='" + txtcity.Text + "',Mob='" + txtmob.Text + "' where Id='" + id + "'";
          con.Open();
          SqlCommand cmd = new SqlCommand(selquery, con);
          cmd.CommandType = CommandType.Text;
          cmd.ExecuteNonQuery();
          con.Close();
          gvdetails.EditIndex = -1;
          gridvalues();
        }
        //on row Gridview Adding Rows
        protected void OnRowCommand_gvdetails(object sender, GridViewCommandEventArgs e)
        {


            TextBox txtfname = (TextBox)gvdetails.FooterRow.FindControl("txtfname");
            TextBox txtfcity = (TextBox)gvdetails.FooterRow.FindControl("txtfcity");
            TextBox txtfmob = (TextBox)gvdetails.FooterRow.FindControl("txtfmob");
            TextBox txtfid = (TextBox)gvdetails.FooterRow.FindControl("txtfid");
          
            string selquery ="insert into Tb_Employee values('"+txtfname.Text+"','"+txtfid.Text+"','"+txtfcity.Text+"','"+txtfmob.Text+"')";
            con.Open();
            SqlCommand cmd = new SqlCommand(selquery, con);
            cmd.CommandType = CommandType.Text;
            cmd.ExecuteNonQuery();
            con.Close();
            gvdetails.EditIndex = -1;
            gridvalues();
           

        }
        private void FnAlert(string msg)
        {
            string strMsg = "alert('" + msg + "')";
            string popupScript = "<script language='javascript'> {" + strMsg + "}</script>";
            Page.ClientScript.RegisterStartupScript(this.GetType(), "PopupScript", popupScript);
        }
        protected void btnsubmit_Click(object sender, EventArgs e)
        {
            int a = 1;
            if (a == 1)
            {
                FnAlert("Hello Naveen");
              
             
            }
        }
     
    }

  
}


No comments:

Post a Comment