Add,Insert,Update,Delete data Inside Gridview

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>  
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  3. <html  
  4.     xmlns="http://www.w3.org/1999/xhtml">  
  5.     <head runat="server">  
  6.         <title></title>  
  7.     </head>  
  8.     <body>  
  9.         <form id="form1" runat="server">  
  10.             <div>  
  11.                 <asp:GridView ID="Gridview1" DataKeyNames="ID" runat="server" ShowFooter="true" OnRowCommand="RowCommand" AutoGenerateColumns="false" OnRowCancelingEdit="RowCancelingEdit" OnRowEditing="RowEditing" OnRowUpdating="RowUpdating" OnRowDeleting="RowDeleting" CellPadding="4" RowStyle-BackColor="Aquamarine"HeaderStyle-BackColor="AliceBlue">  
  12.                     <Columns>  
  13.                         <asp:TemplateField>  
  14.                             <ItemTemplate>  
  15.                                 <asp:Button ID="Button_edit" runat="server" Text="Edit" CommandName="Edit" />  
  16.                                 <asp:Button ID="Button_Delete" runat="server" Text="Delete" CommandName="Delete" />  
  17.                             </ItemTemplate>  
  18.                             <EditItemTemplate>  
  19.                                 <asp:Button ID="Button_update" runat="server" Text="Update" CommandName="Update" />  
  20.                                 <asp:Button ID="Button_Cancel" runat="server" Text="Cancel" CommandName="Cancel" />  
  21.                             </EditItemTemplate>  
  22.                             <FooterTemplate>  
  23.                                 <asp:Button ID="Button_Add" runat="server" Text="Add" CommandName="Add" />  
  24.                             </FooterTemplate>  
  25.                         </asp:TemplateField>  
  26.                         <asp:TemplateField HeaderText="ID">  
  27.                             <ItemTemplate>  
  28.                                 <asp:Label ID="label1_ID" runat="server" Text='<%#Eval("ID")%>'>  
  29.                                 </asp:Label>  
  30.                             </ItemTemplate>  
  31.                             <EditItemTemplate>  
  32.                                 <asp:Label ID="label1_ID" runat="server" Text='<%#Eval("ID")%>'>  
  33.                                 </asp:Label>  
  34.                             </EditItemTemplate>  
  35.                             <FooterTemplate>  
  36.                                 <asp:TextBox ID="Add_Text" runat="server"></asp:TextBox>  
  37.                             </FooterTemplate>  
  38.                         </asp:TemplateField>  
  39.                         <asp:TemplateField HeaderText="Name">  
  40.                             <ItemTemplate>  
  41.                                 <asp:Label ID="label_Name" runat="server" Text='<%#Eval("Name")%>'>  
  42.                                 </asp:Label>  
  43.                             </ItemTemplate>  
  44.                             <EditItemTemplate>  
  45.                                 <asp:TextBox ID="ID_Text1" runat="server" Text='<%#Eval("Name") %>'>  
  46.                                 </asp:TextBox>  
  47.                             </EditItemTemplate>  
  48.                             <FooterTemplate>  
  49.                                 <asp:TextBox ID="Add_Text1" runat="server"></asp:TextBox>  
  50.                             </FooterTemplate>  
  51.                         </asp:TemplateField>  
  52.                         <asp:TemplateField HeaderText="Address">  
  53.                             <ItemTemplate>  
  54.                                 <asp:Label ID="label_Address" runat="server" Text='<%#Eval("Address")%>'>  
  55.                                 </asp:Label>  
  56.                             </ItemTemplate>  
  57.                             <EditItemTemplate>  
  58.                                 <asp:TextBox ID="ID_Text2" runat="server" Text='<%#Eval("Address") %>'>  
  59.                                 </asp:TextBox>  
  60.                             </EditItemTemplate>  
  61.                             <FooterTemplate>  
  62.                                 <asp:TextBox ID="Add_Text2" runat="server"></asp:TextBox>  
  63.                             </FooterTemplate>  
  64.                         </asp:TemplateField>  
  65.                     </Columns>  
  66.                 </asp:GridView>  
  67.             </div>  
  68.         </form>  
  69.     </body>  
  70. </html>  
C# Codings
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Data;  
  7. using System.Web.UI.WebControls;  
  8. using System.Data.SqlClient;  
  9.   
  10. public partial class _Default: System.Web.UI.Page  
  11. {  
  12.     protected void Page_Load(object sender, EventArgs e)   
  13.     {  
  14.         if (!IsPostBack)  
  15.         {  
  16.             binddata();  
  17.         }  
  18.     }  
  19.   
  20.     protected void binddata()   
  21.     {  
  22.         DataTable dt = new DataTable();  
  23.         SqlConnection con = new SqlConnection(@  
  24.         "Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Karthik\Documents\Visual Studio 2010\WebSites\WebSite3\App_Data\Database.mdf;Integrated Security=True;User Instance=True;");  
  25.         con.Open();  
  26.         SqlDataAdapter sda = new SqlDataAdapter("select * from Table1", con);  
  27.         sda.Fill(dt);  
  28.         Gridview1.DataSource = dt;  
  29.         Gridview1.DataBind();  
  30.         con.Close();  
  31.     }  
  32.     protected void RowEditing(object sender, System.Web.UI.WebControls.GridViewEditEventArgs e)   
  33.     {  
  34.         Gridview1.EditIndex = e.NewEditIndex;  
  35.         binddata();  
  36.   
  37.     }  
  38.     protected void RowCancelingEdit(object sender, System.Web.UI.WebControls.GridViewCancelEditEventArgs e)   
  39.     {  
  40.   
  41.         Gridview1.EditIndex = -1;  
  42.         binddata();  
  43.   
  44.     }  
  45.   
  46.     protected void RowCommand(object sender, System.Web.UI.WebControls.GridViewCommandEventArgs e)   
  47.     {  
  48.         if (e.CommandName.Equals("Add"))  
  49.         {  
  50.             TextBox ID = Gridview1.FooterRow.FindControl("Add_Text"as TextBox;  
  51.             TextBox Name = Gridview1.FooterRow.FindControl("Add_Text1"as TextBox;  
  52.             TextBox Address = Gridview1.FooterRow.FindControl("Add_Text2"as TextBox;  
  53.             SqlConnection con = new SqlConnection(@  
  54.             "Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Karthik\Documents\Visual Studio 2010\WebSites\WebSite3\App_Data\Database.mdf;Integrated Security=True;User Instance=True;");  
  55.             con.Open();  
  56.             SqlCommand cmd = new SqlCommand();  
  57.             cmd = new SqlCommand("insert into Table1(ID,Name,Address) values('" + ID.Text + "','" + Name.Text + "','" + Address.Text + "')", con);  
  58.             cmd.ExecuteNonQuery();  
  59.             con.Close();  
  60.             Gridview1.EditIndex = -1;  
  61.             binddata();  
  62.         }  
  63.   
  64.     }  
  65.   
  66.     protected void RowDeleting(object sender, System.Web.UI.WebControls.GridViewDeleteEventArgs e)   
  67.     {  
  68.         int ID = Convert.ToInt32(Gridview1.DataKeys[e.RowIndex].Values["ID"].ToString());  
  69.         TextBox Name = Gridview1.Rows[e.RowIndex].FindControl("ID_Text1"as TextBox;  
  70.         TextBox Address = Gridview1.Rows[e.RowIndex].FindControl("ID_Text2"as TextBox;  
  71.         SqlConnection con = new SqlConnection(@  
  72.         "Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Karthik\Documents\Visual Studio 2010\WebSites\WebSite3\App_Data\Database.mdf;Integrated Security=True;User Instance=True;");  
  73.         con.Open();  
  74.         SqlCommand cmd = new SqlCommand("delete from Table1 where ID=" + ID, con);  
  75.         cmd.ExecuteNonQuery();  
  76.         con.Close();  
  77.         binddata();  
  78.     }  
  79.     protected void RowUpdating(object sender, System.Web.UI.WebControls.GridViewUpdateEventArgs e)   
  80.     {  
  81.         int ID = Convert.ToInt32(Gridview1.DataKeys[e.RowIndex].Values["ID"].ToString());  
  82.         TextBox Name = Gridview1.Rows[e.RowIndex].FindControl("ID_Text1"as TextBox;  
  83.         TextBox Address = Gridview1.Rows[e.RowIndex].FindControl("ID_Text2"as TextBox;  
  84.         SqlConnection con = new SqlConnection(@  
  85.         "Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Karthik\Documents\Visual Studio 2010\WebSites\WebSite3\App_Data\Database.mdf;Integrated Security=True;User Instance=True;");  
  86.         con.Open();  
  87.         SqlCommand cmd = new SqlCommand();  
  88.         cmd = new SqlCommand("update Table1 set Name='" + Name.Text + "',Address='" + Address.Text + "'where ID=" + ID, con);  
  89.         cmd.ExecuteNonQuery();  
  90.         con.Close();  
  91.         Gridview1.EditIndex = -1;  
  92.         binddata();  
  93.     }  
  94. }  
Demo