The gridview fires the RowEditing event whenever the mode
changes from read-only to displaying an edit interface. To invoke editing,
click a button with the CommandName set to Edit. The grid needs three things:
the AutoGenerateEditButton property needs set to true, the RowEditing event
needs an event handler attached to it, and the grid needs rebound with the
EditIndex property set to the current row (before the data bind). Take a look
at the approach below. This event is wired up to each grid in the accordion.
Listing 12
protected void gvwOrders_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView grid = (GridView)sender;
grid.EditIndex = e.NewEditIndex;
HiddenField customerKeyField = (HiddenField)grid.FindControl("hdnCustomerKey");
this.BindGrid(grid, new Guid(customerKeyField.Value));
}
Notice that the method pulls the key from the hidden field
setup in the accordion template, and uses the same BindGrid method to
repopulate the grid.
Update works in a similar manner; any updates need to be
performed manually, as shown below.
Listing 13
protected void gvwOrders_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridView grid = (GridView)sender;
GridViewRow row = grid.Rows[e.RowIndex];
string totalAmount = ((ITextControl)row.Cells[1].Controls[0]).Text;
string referenceNumber = ((ITextControl)row.Cells[2].Controls[0]).Text;
string createdDate = ((ITextControl)row.Cells[3].Controls[0]).Text;
//Update the record
}
Each field to update is extracted by retrieving the value
from the textboxes that hold the value. I did not show it above, but the values
pulled back from the textboxes are passed to the customer record for the
selected customer. This record is then submitted to the database.