0

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.

Community
  • 1
  • 1
FlashTrev
  • 507
  • 6
  • 16
  • When you hit cancel there is a postback hapenning. I would try to remove : if (!IsPostBack) { ... } else { } and I would leave SqlDataSource1.DataBind(); GridView1.DataBind(); let me know if this is a solution – Alexandre Jun 09 '16 at 20:13
  • How are you populating data into the DetailsView and updating it? please show that code – naveen Jun 10 '16 at 11:32
  • Thanks for your comments, adding it to the root of Page_Load, just stopped the gridview from being selectable. I've added my answer below – FlashTrev Jun 11 '16 at 15:43
  • @naveen, its a bit long to add the code here as I have a separate SQLDatasource for the DetailsView that OnSelecting it fires an event to get the IDKey value for a select command that is SelectCommand="SELECT * FROM [DriversID] WHERE DriverIDKey = @DriverID". I also have a Control parameter that links to the GridiView plus some code behind events. I can share it if your interested for yourself but if its just to help find an answer I all ready have it. Thanks. – FlashTrev Jun 11 '16 at 15:50

1 Answers1

0

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.

FlashTrev
  • 507
  • 6
  • 16