GridView1.EditIndex = -1无法正常工作

时间:2016-05-12 10:26:33

标签: c# asp.net gridview

我有一个在编辑模式下工作的GridView,但在更新后,它不会回到正常(非编辑)模式。 gridview.EditIndex = -1无效。

这是设计:

<asp:TemplateField ItemStyle-Width="" HeaderText="Action">
    <EditItemTemplate>
        <asp:LinkButton ID="lnkUpdate" runat="server" CommandName="Update" CssClass="btn btn-xs btn-primary"
            Text="<i class='fa fa-pencil-square-o' style='color:black'></i>  Update"></asp:LinkButton>
        <asp:LinkButton ID="lnlCancel" runat="server" CommandName="Cancel" CssClass="btn btn-xs btn-success"
            Text="<i class='fa fa-ban' style='color:black'></i>  Cancel"></asp:LinkButton>
    </EditItemTemplate>
    <ItemTemplate>
        <asp:LinkButton ID="lnkedit" runat="server" CssClass="btn btn-xs btn-primary" Text="<i class='fa fa-pencil' style='color:black'></i>  Edit"
            CommandName="Edit"></asp:LinkButton>
        <asp:LinkButton ID="lnkNotify" runat="server" CssClass="btn btn-xs btn-success"
            CommandArgument='<%# Eval("ID")%>'
            OnClientClick="return confirm('Do you want to send notification?')"
            Text="<i class='fa fa-bell-o' style='color:black'></i>  Notify" OnClick="Notify"></asp:LinkButton>
        <asp:LinkButton ID="lnkRemove" runat="server" CssClass="btn btn-xs btn-danger"
            CommandArgument='<%# Eval("ID")%>'
            OnClientClick="return confirm('Do you want to delete?')"
            Text="<i class='fa fa-trash-o' style='color:black'></i>  Delete" OnClick="Delete"></asp:LinkButton>
    </ItemTemplate>
</asp:TemplateField>

这是C#代码:

protected void UpdateInfo(object sender, GridViewUpdateEventArgs e)
{
    try
    {
        GridViewRow row = gv.Rows[e.RowIndex];
        new dalSchool().UpdateLandInfo(row, Session["Uid"].ToString());
        BindData();
        gv.EditIndex = -1;
        MessageController.Show(MessageCode.UpdateSucceeded, MessageType.Confirmation, Page);
    }
    catch (Exception ex)
    {
        MessageController.Show(MessageCode.UpdateFailed, MessageType.Error, Page);
    }
}

1 个答案:

答案 0 :(得分:0)

您必须在绑定GridView之前设置EditIndex

protected void UpdateInfo(object sender, GridViewUpdateEventArgs e)
{
    try
    {
        GridViewRow row = gv.Rows[e.RowIndex];
        new dalSchool().UpdateLandInfo(row, Session["Uid"].ToString());
        gv.EditIndex = -1;
        BindData();
        MessageController.Show(MessageCode.UpdateSucceeded, MessageType.Confirmation, Page);
    }
    catch (Exception ex)
    {
        MessageController.Show(MessageCode.UpdateFailed, MessageType.Error, Page);
    }
}