Modifying Data in a GridView Web Server Control

Gridview updating data

Note The original primary

In this section we'll examine four examples. In the UpdateMethod and DeleteMethod properties, you specify the names of the methods that perform those operations. The DynamicField controls add client and server validators based on the validation attributes. Looking back at our last two editable GridView examples, consider what would happen if the user entered a value of, say, Sam for the Unit Price of a product.

You ensured validation rules are applied when interacting with the data. The FirstName and LastName properties are both required. The DynamicEntity control renders that editable properties in the class specified in the ItemType property. Let's create a GridView that displays fields from the Northwind Products table, just like we did in the previous demo.

The GridView's editing interface can be manipulated on a column-by-column level. This is a prime case where a DropDownList would be suited for the editing interface. In addition to deleting a GridView's underlying data, another common need is to allow end users to edit the data displayed in a GridView. Note that this entire exercise was accomplished without writing a line of code. Enforce validation requirements The validation attributes that you applied to the FirstName, LastName, and Year properties in the Student class are automatically enforced when updating the data.

Updating data in gridview

Note The original primary key values for the fields specified in the DataKeyNames property are stored in view state. The ObjectDataSource control calls its update method, passing the changes as parameters to the method call. If, on the other hand, CompareAllValues is used, the Update method must accept both the updated and original non-primary key field values along with the original primary key field s. You can access each dictionary using the arguments passed to the GridView control's update or delete events.

In the next tutorial in this series, you will enable sorting, paging, and filtering data. An editable GridView contains an additional column with an Edit button in each row.

With a GridView control, you can also specify the automatic generation of edit and delete buttons. 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.

Most of the work is required in adding a method to the data access layer class to handle the update. If you provide valid values, the update completes successfully. Conclusion In this tutorial, you enabled updating, deleting, and creating data. The data source control uses the values from the Keys, NewValues, and OldValues dictionaries as parameters for the update or delete command.

Enable updating and deleting Enabling users to update and delete records in the database is very similar to the process for retrieving data. 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 OldValues dictionary contains any original values of fields except the key fields, which are included in the Keys dictionary.

If on the otherThe ObjectDataSource control