Gridview Edit,Update and Delete functionality code:
ASPX Code:
<asp:GridView ID=”gvDetails” DataKeyNames=”id” runat=”server” AutoGenerateColumns=”false”
CssClass=”Gridview” HeaderStyle-BackColor=”Blue” Width=”90%” ShowFooter=”true”
HeaderStyle-Font-Bold=”true” HeaderStyle-ForeColor=”White” OnRowCancelingEdit=”gvDetails_RowCancelingEdit”
OnRowDeleting=”gvDetails_RowDeleting” OnRowEditing=”gvDetails_RowEditing” OnRowUpdating=”gvDetails_RowUpdating”
OnRowCommand=”gvDetails_RowCommand”>
<Columns>
<asp:TemplateField>
<EditItemTemplate>
<asp:ImageButton ID=”imgUpdate” CommandName=”Update” runat=”server” ImageUrl=”../images/update.png”
ToolTip=”Update” Height=”40px” Width=”40px” />
<asp:ImageButton ID=”imgbtnCancel” runat=”server” CommandName=”Cancel” ImageUrl=”../images/Cancel.png”
ToolTip=”Cancel” Height=”40px” Width=”40px” />
</EditItemTemplate>
<ItemTemplate>
<asp:ImageButton ID=”imgbtnEdit” CommandName=”Edit” runat=”server” ImageUrl=”../images/Edit.png”
ToolTip=”Edit” Height=”40px” Width=”40px” />
<asp:ImageButton ID=”imgbtnDelete” CommandName=”Delete” Text=”Edit” runat=”server”
ImageUrl=”../images/delete.png” ToolTip=”Delete” Height=”40px” Width=”40px” />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText=”ID”>
<EditItemTemplate>
<asp:Label ID=”lbleditusr” runat=”server” Text=<%#Eval(“id”) %> />
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID=”lblitemUsr” runat=”server” Text=<%#Eval(“id”) %> />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText=”Title”>
<EditItemTemplate>
<asp:TextBox ID=”txt1″ Width=”350″ Height=”30″ runat=”server” Text=<%#Eval(“newstitle”) %> />
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID=”lblcity” runat=”server” Text=<%#Eval(“newstitle”) %> />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText=”Description”>
<EditItemTemplate>
<asp:TextBox ID=”txt2″ TextMode=”MultiLine” with=”450″ Height=”200″ runat=”server”
Text=<%#Eval(“newsdescription”) %> />
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID=”lblDesg” runat=”server” Text=<%#Eval(“newsdescription”) %> />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText=”Date”>
<ItemTemplate>
<asp:Label ID=”lblDate” runat=”server” Text=<%#Eval(“date”) %> />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
C# Code:
protected void gvDetails_RowEditing(object sender, GridViewEditEventArgs e)
{
gvDetails.EditIndex = e.NewEditIndex;
Bind();
}
protected void gvDetails_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int id = Convert.ToInt32(gvDetails.DataKeys[e.RowIndex].Value.ToString());
//string username = gvDetails.DataKeys[e.RowIndex].Values[“id”].ToString();
TextBox txtcity = (TextBox)gvDetails.Rows[e.RowIndex].FindControl(“txt1”);
TextBox txtDesignation = (TextBox)gvDetails.Rows[e.RowIndex].FindControl(“txt2”);
con.Open();
SqlCommand cmd = new SqlCommand(“update table set title=” + txt1.Text + “,description=” + txt2.Text + ” where id=” + id, con);
cmd.ExecuteNonQuery();
con.Close();
lblresult.ForeColor = Color.Green;
lblresult.Text = “Details Updated successfully”;
gvDetails.EditIndex = -1;
Bind();
}
protected void gvDetails_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
gvDetails.EditIndex = -1;
Bind();
}
protected void gvDetails_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int id = Convert.ToInt32(gvDetails.DataKeys[e.RowIndex].Values[“id”].ToString());
//string username = gvDetails.DataKeys[e.RowIndex].Values[“id”].ToString();
con.Open();
SqlCommand cmd = new SqlCommand(“delete from table where id=” + id, con);
int result = cmd.ExecuteNonQuery();
con.Close();
if (result == 1)
{
Bind();
lblresult.Text = “Record deleted”;
}
}