JavaScript Editor jscript editor     Web designer 

Main Page

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:

  • Setting the AutoGenerateEditButton property to true to enable updating and the AutoGenerateDeleteButton property to true to enable deleting.

  • Adding a CommandField with the ShowEditButton property set to true to enable updating and the ShowDeleteButton property set to true to enable deleting.

  • Creating a TemplateField where the ItemTemplate contains command buttons with the CommandName set to "Edit" for updating and "Delete" for deleting. For more information, see Creating a Custom Column in a GridView Web Server Control.

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.

NoteВ В В 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 How a Data Source Control Creates Parameters for Data-bound Fields.

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 rebinds to the data source control to display the updated data.

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

JavaScript Editor jscript editor     Web designer