1.14. 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 1-17, and an example of edit mode output is shown
in Figure 1-18. Example 1-39 through Example 1-41 show the
.aspx and code-behind files for the application
that produces this result.

Figure 1-17. DataGrid with editing—normal mode

Figure 1-18. 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
EditText, CancelText ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access