Blog Just another tech site

15Sep/121

Asp.net: Update grid view after updating a linked form view

Grid View linked to Form View

A personal project I am currently working on is creating a asp.net database site, which will drive a organizations on-line membership management system.

One of the pages relies on a grid view at the top, which has selectable rows which a form view below relies on. The form view has a data key name of the selected row in the grid view.

When a row / field is updated in the form view, this is not instantly shown in the grid view, but instead will show after a page reload. I first presumed that auto post back would update the grid view, but as it is not the data view that is updated, the system does not automatically update it.

One way I have found to get around this pain, is to create a custom method which rebinds the data source to the grid view after the form view has been updated.

Under the formview object on the aspx page add the following (change the method name as required):

OnItemUpdated="FormView1_ItemUpdated"

Adding on item updated method

The related method in the backend code then needs to be created, add the following code:

protected void FormView1_ItemUpdated(object sender, EventArgs e)
{
this.GridView1.DataBind();
}

Since this is being called after the item has been updated, the datasource will have already been updated, therefore the new data simply needs to be pushed / bound to the grid view object.

Adding the required back-end code

After these changes my grid view now updates instantly without the need to refresh the page.

One response to “Asp.net: Update grid view after updating a linked form view”

  1. Kay says:

    Nice Tut. But I wanted to find out how your formview which is on edit/update mode calculate two textbox in the gridview that auto sum in the third gridview textbox them when the value of either textbox one or two is changed.

Leave a Reply

Your email address will not be published. Required fields are marked *