简体   繁体   中英

Gridview remains in Edit Mode

My Gridview refuses to exit edit mode. When I hit press the Update link, everything works as it should as far as the database being updated, but the Gridview row remains in edit mode. When I press the Cancel button, the Gridview reflects the new information. Why does the row remain in Edit Mode even after setting the EditIndex to -1?

I'm sure it's something really remedial, but I can't find any help with getting a gridview out of edit mode that doesn't just tell me to set the edit index.

protected void Gridview_RowUpdating(object sender, GridViewUpdateEventArgs e) {
    // Update code is here and works fine
    try {
        // this works and updates the database w/ no problems
        ObjectDataSource.Update();
    }
    catch (Exception ex) {
    }

    Gridview.EditIndex = -1;
    Gridview.DataBind();            
}

EDIT: I took out the try/catch and nothing seems to fail, but I do still have to explicitly call the ObjectDataSource.Update() So, here's the setup for my GridView:

<asp:Gridview ID="Gridview" runat="server" DataSourceID="ObjectDataSource" DataKeyNames="ID" OnDataBound="Gridiew_DataBound"
                AutoGenerateColumns="false" OnRowCommand="Gridview_RowCommand" OnRowUpdating="Gridview_RowUpdating">

And the ObjectDataSource

<asp:ObjectDataSource ID="ObjectDataSource" runat="server" TypeName="DAL.Class" SelectMethod="SelectMethod" UpdateMethod="UpdateMethod">
    <UpdateParameters>
        <asp:Parameter Name="param1" Type="Int32" />
        <asp:Parameter Name="param2" Type="String" />
        <asp:Parameter Name="param3" Type="String" />
        <asp:Parameter Name="param4" Type="String" />
        <asp:Parameter Name="param5" Type="String" />
        <asp:Parameter Name="param6" Type="Int32" />
        <asp:Parameter Name="param7" Type="String" />
        <asp:Parameter Name="param8" Type="Int32" />
    </UpdateParameters>
</asp:ObjectDataSource>

So, what is set up incorrectly, that it isn't automatically updating?

EDIT 2: So, I'm now setting the parameter values in the ObjectDataSource_Updating event (which wasn't being called before which is why I had the explicit called to the ods_update()). Now the _Updating event is being hit, but it never goes into my DAL method. Again, no errors are thrown, it just doesn't do anything....

Maybe something bad is happening like the edit is failing. And since you are hiding errors:

catch (Exception ex) {
    }

...you can't see what's happening.

If the GridView/DataSource is setup properly, you don't need to explicitly call Update or DataBind.

Can you give a shot by updating a dummy table? (in Gridview_RowUpdating event)

 ObjectDataSource.UpdateCommand = @"UPDATE somedummytable set  parameter";   
ObjectDataSource.Update();     
Gridview.EditIndex = -1;       
    ObjectDataSource.DataBind();     
Gridview.DataBind();    

After this step, I am sure it will come out of edit mode.

In case this helps anyone, make sure you update your Edit Index after update/cancel:

GridView1.EditIndex = -1

That did it for me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM