Modifying Data in a GridView Web Server Control
Thecontrol has built-in functionality for allowing users to edit or delete records without requiring programming. You can customize the edit or delete functionality of the GridView control using events and templates.
Enabling the Built-In Editing Functionality
You can enable the built-in edit or delete functionality of the GridView control in any of the following ways:
Setting theproperty to true to enable updating and the property to true to enable deleting.
Adding awith the property set to true to enable updating and the property set to true to enable deleting.
Creating awhere the contains command buttons with the set to "Edit" for updating and "Delete" for deleting. For more information, see .
How Editing Works in the GridView Control
The GridView control can display a user interface (UI) to enable users to edit the contents of individual rows. Typically, an editable grid contains a column with a button or link in it that users can click to put the row into edit mode. (By default, the button caption is "Edit.")
When users save a change, the GridView control passes the changes and primary key information to the data source control, identified by theproperty, which invokes the appropriate update operation. For example, the control executes a SQL Update statement using the changed data as parameter values. The control calls its update method, passing the changes as parameters to the method call.
The GridView control passes values to the data source for an update or delete operation in three dictionary collections: the Keys dictionary, the NewValues dictionary, and the OldValues dictionary. You can access each dictionary using the arguments passed to the GridView control's update or delete events.
The Keys dictionary contains the names and values of fields that uniquely identify the record to update or delete, and always contains the original values of the key fields. To specify which fields are placed in the Keys dictionary, set theproperty to a comma-separated list of field names that represent the primary key of your data. The collection is populated automatically with the values associated with the fields specified for the DataKeyNames property.
The original primary key values for the fields specified in the DataKeyNames property are stored in view state. If your primary key values contain sensitive information, you should encrypt the view state contents by setting the page'sproperty to .
The NewValues dictionary contains the current values from the input controls in the row being edited. The OldValues dictionary contains any original values of fields except the key fields, which are included in the Keys dictionary.
The data source control uses the values from the Keys, NewValues, and OldValues dictionaries as parameters for the update or delete command. For information on how data source control parameters are created based on the dictionaries created for bound values, see.
You can examine or customize the contents of any of these dictionaries before they are passed to the data source by handling theor events. After the update or delete is complete, the GridView control raises its or event. These events allow you to perform post-query logic such as integrity checks.
After the update or deletion is complete and all events have been raised, the GridView control rebinds to the data source control to display the updated data.
The original values for updatable fields in a GridView control are stored in. If ViewState is disabled on an ASP.NET page that includes an updatable GridView control, then optimistic concurrency checks cannot use the original values for updatable and primary key fields that were retrieved when the GridView control was first bound to the data source. When the page posts back in order to perform an update or delete, current values from the database are retrieved as the original values for the updatable and primary key fields in the GridView control because no values are stored in ViewState. The update or delete operation is then performed using these original values. If the original values have changed since the GridView control was first populated, the update or delete will succeed, but the optimistic concurrency check will not report a failure as would be expected.
Customizing the Editing User Interfaces in the GridView Control
You can customize the editing (UI) elements, such as the type of control displayed in edit mode for every data field. Automatic two-way data binding allows your custom control to provide the editable and edited values, to and from the data store.
If you change the update statement in a data source control or if you rearrange the columns in the GridView control, always ensure that the values the GridView control passes to the data source match the corresponding data source configuration.