2.13. Editing Data Within a DataGrid


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


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


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"
							  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.


The Edit command button’s EditText, CancelText ...

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

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.