Modifying Data in a GridView Web Server Control

The GridView control 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:

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 the DataSourceID property, which invokes the appropriate update operation. For example, the SqlDataSource control executes a SQL Update statement using the changed data as parameter values. The ObjectDataSource 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 the DataKeyNames property to a comma-separated list of field names that represent the primary key of your data. The DataKeys 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's ViewStateEncryptionMode property to Always.

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 Using Parameters with Data Source Controls for Inserting and Updating.

You can examine or customize the contents of any of these dictionaries before they are passed to the data source by handling the RowUpdating or RowDeleting events. After the update or delete is complete, the GridView control raises its RowUpdated or RowDeleted 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 ViewState. 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.

See Also


GridView Web Server Control Overview