Use the EditCommandColumn class to create a special column for the DataGrid control that contains the Edit, Update, and Cancel buttons for each data row in the grid. These buttons allow you to edit the values of a row in the DataGrid control.
If no row is selected, an Edit button displays in the EditCommandColumn object for each data row in the DataGrid control. When the Edit button for an item is clicked, the EditCommand event is raised and the Edit button is replaced with the Update and Cancel buttons. You need to provide code to handle the EditCommand event. A typical event handler sets the EditItemIndex property to the selected row, and then rebinds the data to the DataGrid control.
Note: |
|---|
You must provide values for the
CancelText, EditText, and UpdateText properties. Otherwise, the associated buttons will not appear in the EditCommandColumn.
|
The buttons in the EditCommandColumn can be set to display as hyperlinks or push buttons by setting the ButtonType property.
Clicking the Update or Cancel button raises the UpdateCommand or CancelCommand event, respectively. You need to provide code to handle these events.
A typical handler for the UpdateCommand event updates the data, sets the EditItemIndex property to -1 (to deselect the item), and then rebinds the data to the DataGrid control.
A typical handler for the CancelCommand event sets the EditItemIndex property to -1 (to deselect the item), and then rebinds the data to the DataGrid control.
By default, page validation is performed when an Update button in the EditCommandColumn control is clicked. Page validation determines whether the input controls that are associated with a validation control on the page all pass the validation rules that are specified by the validation control. To prevent page validation from occurring, set the CausesValidation property to false.