Resources » .NET programming » ASP.NET/Web Applications

How to Edit,Cancel,Update,Delete in Gridview


Posted Date: 28-Aug-2009  Last Updated:   Category: ASP.NET/Web Applications    
Author: Member Level: Gold    Points: 10



Update, Delete, Cancel in Gridview



In this Article you can learn how to edit, update, delete, and cancel in gridview.

First drag and drop Gridview.In gridview properties set AutoGenarateColumns to False.
Next open Default.aspx source code.
To make a columns in Gridview use

<asp:TemplateField>
</asp:TemplateField>


Here first I created a table name 'emp' in my database.it contains 3 colomns as id,name,marks,I am creating this colomns in gridview as follows:



<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Style="z-index: 100;left: 298px; position: absolute; top: 118px">
<Columns >
<asp:TemplateField HeaderText ="IDNO">
<ItemTemplate>
<asp:LabelID="lblid"runat="server"Text='<%#Eval("rowid")%>'></asp:Labe>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name" >
<ItemTemplate > <%#Eval("name") %></ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText ="Marks">
<ItemTemplate><%#Eval("marks") %></ItemTemplate>
</asp:TemplateField>

</Columns>
</asp:GridView>


In default.asp.cs page fill the gridview using SqlDataAdapter using below code:

SqlDataAdapter da = new SqlDataAdapter("select * from emp", conn);
DataSet ds = new DataSet();
da.Fill(ds, "emp");
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
conn.Close();


Next in Default.asp page select your gridview in Properties set AutoGenarateDeleteButton=True and AutoGenarateEditButton=True.

Next we have to write a code for editing,updating,cancel:In Default.aspx source code we have to add EditItemTemplate element

This EditItemTemplate is used to Edit the Row in Gridview.Here I am going to Edit only two columns name and marks.

<Columns >
<asp:TemplateField HeaderText ="IDNO"><ItemTemplate ><asp:Label ID="lblid" runat ="server" Text ='<%#Eval("rowid") %>' ></asp:Label></ItemTemplate></asp:TemplateField>

<asp:TemplateField HeaderText="Name" ><ItemTemplate > <%#Eval("name") %></ItemTemplate>
<EditItemTemplate >
<asp:TextBox ID="textbox1" runat ="server" Text ='<%#Eval("name") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText ="Marks"><ItemTemplate><%#Eval("marks") %> </ItemTemplate>
<EditItemTemplate >
<asp:TextBox ID="textbox2" runat ="server" Text ='<%#Eval("marks") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>

</Columns>


For Editing a Gridview:



In Gridview Events:Double Click on RowEditing Event and write below code

protected void GridView1_RowEditing(object sender,GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
bind();
}
When you click on Edit link it shows Update,Cancel links.


For Updating a Gridview:


Updating link is used to update a Particular row in emp table using Gridview.
Double click on RowUpdating Event and write below code

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
Label lbl = (Label)row.FindControl("lblid");
TextBox textname = (TextBox)row.FindControl("textbox1");
TextBox textmarks = (TextBox)row.FindControl("textbox2");
GridView1.EditIndex = -1;
conn.Open();
SqlCommand cmd = new SqlCommand("update emp set marks=" + textmarks.Text + " , name='" + textname.Text + "' where rowid=" + lbl.Text + "", conn);
cmd.ExecuteNonQuery();
conn.Close();
bind();
}


For Canceling a gridview row Operation:



Cancel Link in used to cancel the particular row operation before upadating.when you click on Gricview it goes in first stage.

Double click on RowCancelingEdit Event and wrtie below code

protected void GridView1_RowCancelingEdit(object sender,GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
bind();
}


For Deleting a Gridview row:


Delete Link is used to delete a Row in a emp table.it permanently deletes a particular Row From GridView
Double Click on RowDeleting Event and write below code


protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
Label lbldeleteID = (Label)row.FindControl("lblid");
conn.Open();
SqlCommand cmd = new SqlCommand("delete emp where rowid=" + lbldeleteID.Text + "", conn);
cmd.ExecuteNonQuery();
conn.Close();
bind();
}


The complete code in .aspx.cs page is


using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
SqlConnection conn;
protected void Page_Load(object sender, EventArgs e)
{
conn = new SqlConnection("Data Source=INTHIYAAZ;Initial Catalog=shakeer;uid=sa;pwd=sa;");
if(!IsPostBack )
{

bind();

}

}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e) {
GridView1.EditIndex = e.NewEditIndex;
bind();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
bind();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) {
GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
Label lbl = (Label)row.FindControl("lblid");

TextBox textname = (TextBox)row.FindControl("textbox1");
TextBox textmarks = (TextBox)row.FindControl("textbox2");
GridView1.EditIndex = -1;
conn.Open();
SqlCommand cmd = new SqlCommand("update emp set marks=" + textmarks.Text + " , name='" + textname.Text + "' where rowid=" + lbl.Text + "", conn);
cmd.ExecuteNonQuery();
conn.Close();
bind();
}
public void bind()
{
conn.Open();
SqlDataAdapter da = new SqlDataAdapter("select * from emp", conn); DataSet ds = new DataSet();
da.Fill(ds, "emp");
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
conn.Close();
}


protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
Label lbldeleteID = (Label)row.FindControl("lblid");
conn.Open();
SqlCommand cmd = new SqlCommand("delete emp where rowid=" + lbldeleteID.Text + "", conn);
cmd.ExecuteNonQuery();
conn.Close();
bind();
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
bind();
}
}


Did you like this resource? Share it with your friends and show your love!

Responses to "How to Edit,Cancel,Update,Delete in Gridview"
Guest Author: Panggih     20 Feb 2012
Your story was really informative, thanks!


Feedbacks      

Post Comment:




  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)



    Type the numbers and letters shown on the left.


    Submit Article     Return to Article Index

    Subscribe to Subscribers
    Active Members
    TodayLast 7 Daysmore...

    Awards & Gifts
    Talk to Webmaster Tony John

    Online Members

    Priya jain
    More...
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India