2.13. Editing Data Within a DataGrid

Problem

You want to allow the user to edit the data within the table displayed by a DataGrid.

Solution

Add an EditCommandColumn column type to the DataGrid control’s display to enable editing of the data fields of each record. A typical example of normal display mode output is shown in Figure 2-14, and an example of edit mode output is shown in Figure 2-15. Examples 2-36, 2-37 through 2-38 show the .aspx and code-behind files for the application that produces this result.

DataGrid with editing—normal mode

Figure 2-14. DataGrid with editing—normal mode

DataGrid with editing—row edit mode

Figure 2-15. DataGrid with editing—row edit mode

Discussion

This recipe uses the built-in editing facilities of the DataGrid control, in particular the EditCommandColumn column type, which provides Edit command buttons for editing data items in each row of a DataGrid. The EditText, CancelText, and UpdateText properties define the text to be output for the Edit command button’s Edit, Cancel, and Update hyperlinks, respectively.

	<asp:EditCommandColumn ButtonType="LinkButton"
							  EditText="Edit"
							  CancelText="Cancel"
							  UpdateText="Update" />

The ButtonType attribute defines the type of button to output. You can specify LinkButton, which provides hyperlinked text, or PushButton, which outputs an HTML button.

Tip

The Edit command button’s ...

Get ASP.NET 2.0 Cookbook, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.