GridView not data binding on postback of DetailsView

732 Views Asked by At

I have the same question as:

GridView contents don’t update when underlying data changes

but the answers supplied do not work for me, i'm after ideas please because so far I've wasted 3 days trying to get a GridView to refresh on the postback of a DetailsView.

Situation is this:-

I have a GridView that when a row is selected a DetailsView displays the detailed info. in.

On clicking edit the DetailsView goes into 'Edit' mode.

I edit it and click the Update button.

The Update fires an event and it correctly updates my SQL database table.

The issue is despite 100's of posts saying use GridView1.Databind(); it will not refresh till I click the Cancel button.

I know its posting back because I have debugged it and seen it in Page_load(...) postback.

I have added

SqlDataSource1.DataBind();

and

GridView1.DataBind()

to the following places and none seem to refresh my GridView.

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        ...
    }
    else
    {
    SqlDataSource1.DataBind();
    GridView1.DataBind();
    }
}

also

protected void DetailsView1_ItemUpdating(object sender,   DetailsViewUpdateEventArgs e)
{
   GridView1.DataBind();
}

also

protected void DetailsView1_ItemUpdated(object sender, DetailsViewUpdatedEventArgs e)
{
    //GridView.SelectedIndex = -1;
   SqlDataSource1.EnableCaching = false;
    // UpdatePanel14.Update();
    SqlDataSource1.DataBind();
    GridView1.DataBind();
    SqlDataSource1.EnableCaching = true;
    // EndEditingGridView();
}

So as you can see i've tried a postback under Page_Load(), on Updating and Updated of the DetailsView too. I've also tried setting 'ViewState = Disabled' on the GridView too. Nothing seems to get it to update its contents unless I click the 'Cancel' button. Thanks.

1

There are 1 best solutions below

0
On

I found the answer from some previous work I had done, What I have found works is to remove the Gridviews Datasource, reset its Index and databind it. Then Update the UpdatePanel, then re-connect the gridview and databind it again.

My Function, which I call at the end of my DetailsView1_ItemUpdated() event:

private void EndEditingDetailsView1()
{
    GridView.DataSourceID = null;
    GridView.EditIndex = -1;
    GridView.DataBind();

    DetailsUpdatePanel.DataBind();
    DetailsUpdatePanel.Update(); 

    GridView.DataSourceID = "SqlDataSource";
    GridView.EditIndex = -1;
    GridView.DataBind();
}

works a treat.