Asp.Net

Asp.Net Gridview Edit,Delete,Update and Adding Rows Implementation

Asp.net Aspx Page:
File Name:-Default.aspx
 <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>
-------------------------------------------------------------------------------------------------------------
In Code Behind Page Below Code:
File Name:-Default.aspx.cs

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
=newSqlConnection(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();
           

        }

     
    }

  
}



  

No comments:

Post a Comment