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 from the toolbox to Default.aspx page. Now in gridview properties set AutoGenarateColumns to False.
<asp:gridview autogeneratecolumns="False" runat="server" id="GridView1">
<columns>
<asp:templatefield headertext="IDNO">
<itemtemplate>
<asp:Label ID="lblid" runat="server" Text='<%#Eval("rowid")%>' />
</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>
Next open Default.aspx source code.
Now create a table name 'emp' in database that contains 3 colomns rowid,name,marks.
In default.asp.cs page fill the gridview with the existing data in table emp.
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 to edit and delete records from the grid in set AutoGenarateDeleteButton=True and AutoGenarateEditButton=True.
Figure 1
Next we have to write a code for editing,updating,cancel:In Default.aspx source code we have to add
This is used to Edit the Row in Gridview.Here I am going to Edit only two columns name and marks.
Figure 2
For Editing a Gridview:
Fogure 3
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:
Figure 4
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 belwo 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 is written as follows
and in your .cs page
SqlConnection conn;
protected void Page_Load(object sender, EventArgs e)
{
conn = new SqlConnection("Data Source=MyDB;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();
}