user1181942
user1181942

Reputation: 1607

Grid view Edit issue

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?

Upvotes: 0

Views: 905

Answers (1)

Rumit Parakhiya
Rumit Parakhiya

Reputation: 2714

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:

<asp:GridView ID="objGridView" runat="server" AutoGenerateColumns="false" onRowEditing="objGridView_RowEditing">
  <columns>
     <asp:TemplateField HeaderText="">
        <ItemTemplate>
          <asp:Label ID="lblEmailID" runat="server" Text='<%#Eval("<<EmailID Field>>")%>' />
        </ItemTemplate>
        <EditItemTemplate>
          <asp:TextBox ID="txtEmailID" runat="server" Text='<%#Eval("<<EmailID Field>>")%>'/>
        </EditItemTemplate>
     </asp:TemplateField>
  </columns>
</asp:GridView>

Code Behind:

protected void objGridView_RowEditing(object sender, GridViewEditEventArgs e)
{
  objGridView.EditIndex = e.NewEditIndex;
  <<BindGrid Again>>
}

Upvotes: 2

Related Questions