Gridview Edit Delete Update
In this tutorial i will let you know about the Edit, Delete, Update functionality in Gridview using C#.In this we are using Gridview Events like Gridview_updating, Gridview_Deleteing, Gridview_CancelingEdit.
For this we start from creating a connection.How to create a connection in asp.net you can find that on following post here.
Design View:-
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!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></title> </head> <body> <form id="form1" runat="server"> <div> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" onrowcancelingedit="GridView1_RowCancelingEdit" onrowdeleting="GridView1_RowDeleting" onrowediting="GridView1_RowEditing" onrowupdating="GridView1_RowUpdating"> <Columns> <asp:TemplateField HeaderText="ID"> <ItemTemplate> <asp:Label ID="lid" runat="server" Text='<%#Eval("id") %>'>'> </asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Name"> <ItemTemplate> <asp:Label ID="name" runat="server" Text='<%#Eval("name") %>'>'> </asp:Label> </ItemTemplate> <EditItemTemplate> <asp:TextBox ID="txtname" runat="server" Text='<%#Eval("name") %>'>' ></asp:TextBox> </EditItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Class"> <ItemTemplate> <asp:Label ID="class" runat="server" Text='<%#Eval("class") %>'>'> </asp:Label> </ItemTemplate> <EditItemTemplate> <asp:TextBox ID="txtclass" runat="server" Text='<%#Eval("class") %>'>' ></asp:TextBox> </EditItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Action"> <ItemTemplate> <asp:LinkButton ID="edit" Text="Edit" runat="server" CommandName="Edit"></asp:LinkButton> <asp:LinkButton ID="Delete" Text="Delete" runat="server" CommandName="Delete"></asp:LinkButton> </ItemTemplate> <EditItemTemplate> <asp:LinkButton ID="Update" Text="Update" runat="server" CommandName="Update"></asp:LinkButton> <asp:LinkButton ID="Cancel" Text="cancel" runat="server" CommandName="Cancel"></asp:LinkButton> </EditItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> </div> </form> </body> </html>
Code View:-
using System; using System.Collections.Generic; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data; using System.Data.SqlClient; using System.Configuration; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { binddata(); } } public void binddata() { string strcon = ConfigurationManager.ConnectionStrings["con"].ConnectionString; SqlConnection conn = new SqlConnection(strcon); conn.Open(); string strquery = "select id,name,class from flatfile_logged"; SqlCommand cmd = new SqlCommand(strquery,conn); SqlDataAdapter da = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); da.Fill(ds); GridView1.DataSource = ds; GridView1.DataBind(); conn.Close(); } protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e) { string lid = ((Label)GridView1.Rows[e.RowIndex].FindControl("lid")).Text; string strcon = ConfigurationManager.ConnectionStrings["con"].ConnectionString; SqlConnection conn = new SqlConnection(strcon); conn.Open(); String strsession1 = "delete from flatfile_logged where id='"+lid+"'"; SqlCommand cmd = new SqlCommand(strsession1, conn); cmd.ExecuteNonQuery(); conn.Close(); binddata(); } protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) { string lid = ((Label)GridView1.Rows[e.RowIndex].FindControl("lid")).Text; string name = ((TextBox)GridView1.Rows[e.RowIndex] .FindControl("txtname")).Text; string class1 = ((TextBox)GridView1.Rows[e.RowIndex] .FindControl("txtclass")).Text; string strcon = ConfigurationManager.ConnectionStrings["con"].ConnectionString; SqlConnection conn = new SqlConnection(strcon); conn.Open(); String strsession = "update flatfile_logged set name='" + name.ToString() + "', class ='" + class1.ToString() + "' where id='" + lid + "'"; SqlCommand cmd = new SqlCommand(strsession, conn); cmd.ExecuteNonQuery(); GridView1.EditIndex = -1; conn.Close(); binddata(); } protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) { GridView1.EditIndex = -1; binddata(); } protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e) { GridView1.EditIndex = e.NewEditIndex; binddata(); } }