Partilhar via


Modificando Dados em um Controle do Servidor Web GridView

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.

Ativando a Funcionalidade de Edição Interna

You can enable the built-in edit or delete functionality of the GridView control in any of the following ways:

Como a Edição Funciona no Controle GridView

The GridView control can display a user interface (UI) to enable users to edit the contents of individual rows.Normalmente, uma grade editável contém uma coluna com um botão ou link dentro dela que os usuários podem clicar para colocar a linha em modo de edição.(Por padrão, a legenda do botão é " 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 controle passa valores para a fonte de dados para uma operação de atualização ou excluir nas três coleções do dicionário: the Keys dicionário, o NewValues dicionário e o OldValues dicionário. 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.

Observação:

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 Como um Controle da Fonte de Dados Cria Parâmetros para Campos Ligados a Dados.

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.

Observação:

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.

Personalizando as Interfaces de Usuário de Edição no Controle GridView

Você pode personalizar os elementos (UI) de edição, como o tipo de controle exibido na edição para cada campo de dados.Vinculação automática de dados bidirecionais permite o controle personalizado para fornecer os valores editáveis e editados, de e para o armazenamento de dados.

Observação:

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.

Consulte também

Conceitos

Visão geral sobre controles fonte de dados

Referência

Visão Geral sobre o Controle do Servidor Web GridView