Hello dude use this code , I have take some modification
in your aspx page .
<asp:GridView ID="gvdetails" runat="server" AutoGenerateColumns="false"
AllowPaging="true" PageSize="5"
OnRowEditing="gvdetails_RowEditing" OnRowDeleting="gvdetails_RowDeleting" OnRowUpdating="gvdetails_RowUpdating" DataKeyNames="UserId,UserName"
OnRowCancelingEdit="gvdetails_RowCancelingEdit" OnPageIndexChanging="gvdetails_PageIndexChanging">
<PagerSettings Mode="NumericFirstLast" PageButtonCount="5" FirstPageText="Firstpage" NextPageText="Nextpage" />
<Columns>
<asp:TemplateField>
<EditItemTemplate>
<asp:ImageButton ID="imgbtnUpdate" CommandName="Update" runat="server" ImageUrl="~/Images/update.jpg" ToolTip="Update" Height="20px" Width="20px" />
<asp:ImageButton ID="imgbtnCancel" CommandName="Cancel" runat="server" ImageUrl="~/Images/Button cancel Icon.jpg" ToolTip="Cancel" Height="20px" Width="20px" />
</EditItemTemplate>
<ItemTemplate>
<asp:ImageButton ID="imgbtnEdit" CommandName="Edit" runat="server" ImageUrl="~/Images/Edit Icon.jpg" ToolTip="Edit" Height="20px" Width="20px" />
<asp:ImageButton ID="imgbtnDelete" CommandName="Delete" runat="server" ImageUrl="~/Images/Delete.jpg" ToolTip="Delete" Height="20px" Width="20px" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="UserName">
<ItemTemplate>
<asp:Label ID="lblitmusr" runat="server" Text='<%#Eval("UserName")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<%-- <asp:Label ID="Label1" runat="server" Text='<%#Eval("UserName") %>'></asp:Label>
<asp:TextBox ID="lbleditusr" runat="server" Text='<%#Eval("UserName") %>'></asp:TextBox> <%--
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="City">
<ItemTemplate>
<asp:Label ID="lblitmcity" runat="server" Text='<%#Eval("City") %>' ></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<%-- <asp:Label ID="Label1" runat="server" Text='<%#Eval("City") %>'></asp:Label>
<asp:TextBox ID="lbleditcity" runat="server" Text='<%#Eval("City") %>'></asp:TextBox> <%--
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Designation">
<ItemTemplate>
<asp:Label ID="lblitmdesignation" runat="server" Text='<%#Eval("Designation")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<%-- <asp:Label ID="lbleditdesignation" runat="server" Text='<%#Eval("Designation") %>'></asp:Label>
<asp:TextBox ID="lbleditdesignation" runat="server" Text='<%#Eval("Designation") %>'></asp:TextBox> <%--
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Label ID="lblresult" runat="server"></asp:Label>
And in your aspx.cs page
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGridView();
}
}
protected void BindGridView()
{
con.Open();
SqlCommand cmd = new SqlCommand("select * from GridDetails", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
con.Close();
if (ds.Tables[0].Rows.Count > 0)
{
gvdetails.DataSource = ds;
gvdetails.DataBind();
}
else
{
ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
gvdetails.DataSource = ds;
gvdetails.DataBind();
int columncount = gvdetails.Rows[0].Cells.Count;
gvdetails.Rows[0].Cells.Clear();
gvdetails.Rows[0].Cells.Add(new TableCell());
gvdetails.Rows[0].Cells[0].ColumnSpan = columncount;
gvdetails.Rows[0].Cells[0].Text = "No Records Found";
}
}
protected void gvdetails_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow row = (GridViewRow)gvdetails.Rows[e.RowIndex];
int userid = Convert.ToInt32(gvdetails.DataKeys[e.RowIndex].Value.ToString());
string username = gvdetails.DataKeys[e.RowIndex].Values["UserName"].ToString();
TextBox txtcity = (TextBox)row.FindControl("lbleditcity");
TextBox txtDesignation = (TextBox)row.FindControl("lbleditdesignation");
con.Open();
SqlCommand cmd = new SqlCommand("update GridDetails set City='" + txtcity.Text + "',Designation='" + txtDesignation.Text + "' where Userid=" + userid, con);
cmd.ExecuteNonQuery();
con.Close();
lblresult.Text = username + "Details Updated Successfully";
gvdetails.EditIndex = -1;
BindGridView();
}
protected void gvdetails_RowEditing(object sender, GridViewEditEventArgs e)
{
gvdetails.EditIndex = e.NewEditIndex;
BindGridView();
}
protected void gvdetails_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
gvdetails.EditIndex = -1;
BindGridView();
}
protected void gvdetails_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int userid = Convert.ToInt32(gvdetails.DataKeys[e.RowIndex].Values["UserId"].ToString());
string username = gvdetails.DataKeys[e.RowIndex].Values["UserName"].ToString();
con.Open();
SqlCommand cmd = new SqlCommand("delete from GridDetails where UserId=" + userid, con);
int result = cmd.ExecuteNonQuery();
con.Close();
if (result == 1)
{
BindGridView();
lblresult.Text = username + "Details deleted successfully";
}
}
protected void gvdetails_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gvdetails.PageIndex = e.NewPageIndex;
BindGridView();
}