I have editable grid view, If I click on edit in any row say “x”, it opens in edit mode, however if I click on any other row, say “y”, “x” should cancel edit. But In my code after clicking on “y”, both the rows remains in edit mode.
protected void gvViewAdmins_RowEditing(object sender, GridViewEditEventArgs e)
{
gvViewAdmins.EditIndex = e.NewEditIndex;
Label lblEmailId = gvViewAdmins.Rows[e.NewEditIndex].FindControl("gvlblEmail") as Label;
lblEmailId.Visible = false;
ViewState["currentEmailId"] = lblEmailId.Text;
TextBox textboxEmailId = gvViewAdmins.Rows[e.NewEditIndex].FindControl("gvtbEmailId") as TextBox;
textboxEmailId.Text = ViewState["currentEmailId"].ToString();
textboxEmailId.Visible = true;
Label lblRole = gvViewAdmins.Rows[e.NewEditIndex].FindControl("gvlblRole") as Label;
lblRole.Visible = false;
ViewState["currentRole"] = lblRole.Text;
DropDownList dropdownRoles = gvViewAdmins.Rows[e.NewEditIndex].FindControl("gvddlRoles") as DropDownList;
this.PopulateRole(dropdownRoles);
dropdownRoles.Visible = true;
this.SelectRoleDropDownValue(dropdownRoles);
LinkButton lbtnUpdate = gvViewAdmins.Rows[e.NewEditIndex].FindControl("lbtnUpdate") as LinkButton;
LinkButton lbtnCancel = gvViewAdmins.Rows[e.NewEditIndex].FindControl("lbtnCancel") as LinkButton;
LinkButton lbtnEdit = gvViewAdmins.Rows[e.NewEditIndex].FindControl("lbtnEdit") as LinkButton;
lbtnUpdate.Visible = true;
lbtnCancel.Visible = true;
lbtnEdit.Visible = false;
}
What is wrong in my code?
After looking at your code, It seems that, you are not using gridview’s default edit functionality. You are just setting controls visible property on and off.
Better way is, Define ItemTemplate and Edit Item Template Separately like,
Markup:
Code Behind: