How to save additional records when saving/updating a DetailsView

It is very common in ASP.NET 2 to have a GridView that is linked to a DetailsView, so that the rows in the GridView are edited in a separate DetailsView, usually on another page or in another part of a MultiView. In this case you may need the DetailsView to do additional processing when a record is saved in it (i.e. when data has been updated) and you will definitely want the details that you saved in the DetailsView to be reflected in the GridView. Here is how to achieve this:

First, use DataBind on the parent GridView during the ItemUpdated event of the DetailsView. This makes sure that the GridView refreshes its view of the data so that it shows the same information that the DetailsVIew has just saved:

protected void DetailsView1_ItemUpdated(object sender, DetailsViewUpdatedEventArgs    e)
{
   GridView1.DataBind();
}

To do additional processing when the DetailsView record has been saved, you may need to get at certain items of the record that has just been saved. You can do this by reading the DataKey values. You can add as many DataKeys to your detailsview as you like, and access them as an array of values in the ItemUpdated event:

protected void DetailsView1_ItemUpdated(object sender, DetailsViewUpdatedEventArgs    e)
{
   string record_id = e.Keys[0].ToString();
} 

In this example, we get the first of the DataKeys of the record, which in this case is the primary key. This could be used to make related updates to other tables in the database.