Online Book Reader

Home Category

Programming Microsoft ASP.NET 4 - Dino Esposito [239]

By Root 5647 0
value is omitted, the column is sorted in ascending order. The following code sets up the GridView column for sorting on the productname data source column:

AllowSorting="true" AutoGenerateColumns="false">

sortexpression="productname" />

headertext="Packaging" />

Just as for paging, with a GridView no manually written code is required to make sorting work. If properly configured, the GridView’s sorting infrastructure works without further intervention and in a bidirectional way—that is, if you click on a column sorted in descending order, it is sorted in ascending order and vice versa. You need to add some custom code only if you want to implement more advanced capabilities such as showing a glyph in the header to indicate the direction. Just as for paging, the main snag with sorting is how the underlying data source control implements it.

Editing Data


A major strength of the GridView control—which makes up for a major shortcoming of the DataGrid—is the ability to handle updates to the data source. The DataGrid control provides only an infrastructure for data editing. The DataGrid provides the necessary user interface elements and fires appropriate events when the user modifies the value of a certain data field, but it does not submit those changes back to the data source. Developers are left with the disappointing realization that they have to write a huge amount of boilerplate code to really persist changes.

With the GridView control, when the bound data source supports updates, the control can automatically perform this operation, thus providing a truly out-of-the-box solution. The data source control signals its capability to update through the CanUpdate Boolean property.

The GridView can render a column of command buttons for each row in the grid. These special command columns contain buttons to edit or delete the current record. With the DataGrid, you must explicitly create an edit command column using a special column type—the EditCommandColumn class. The GridView simplifies things quite a bit for update and delete operations.

In-place editing refers to the grid’s ability to support changes to the currently displayed records. You enable in-place editing on a grid view by turning on the AutoGenerateEditButton Boolean property:

autogeneratecolumns="false" autogenerateeditbutton="true">

...

When the AutoGenerateEditButton property is set to true, the GridView displays an additional column, like that shown in Figure 10-11. By clicking the Edit button, you put the selected row in edit mode and can enter new data at will.

Figure 10-11. A GridView that supports in-place editing.

To abort editing and undo any changes, users simply click the Cancel button. The GridView can handle this click without any external support; the row returns to its original read-only state; and the EditIndex property takes back its –1 default value—meaning no row is currently being edited. But what if users click the update link? The GridView first fires the RowUpdating event and then internally checks the CanUpdate property on the data source control. If CanUpdate returns false, an exception is thrown. CanUpdate returns false if the data source control has no update command defined. The successful completion of an update command is signaled throughout the grid via the RowUpdated event.

The GridView collects values from the input fields and populates a dictionary of name/value pairs that indicate the new values for each field of the row. The GridView also exposes a RowUpdating event that allows the programmer to validate the values being passed to the data source object. In addition, the GridView automatically calls Page.IsValid before starting the update operation on the associated data source. If Page.IsValid returns false,

Return Main Page Previous Page Next Page

®Online Book Reader