1.16. Allowing Selection Anywhere Within a DataGrid Row
Problem
You are implementing a DataGrid that
requires selection of a row, but you do
not want to have a Select button in every row of your
DataGrid. What you really want is to allow the
user to click anywhere within a row, like in a classic Windows
application.
Solution
To every row in the DataGrid add a hidden Select
button along with an onclick event that performs
the same action as if the hidden Select button were clicked.
Add a hidden
ButtonColumnto theDataGrid.Set the
ButtonTypeattribute to "LinkButton" so that a hidden hyperlinked Select button is rendered in every row.In the
ItemDataBoundevent, add anonclickevent to theDataGridrow that performs the same action as clicking the hidden Select button.
The approach produces output like that shown in Figure 1-20. Example 1-45 through
Example 1-47 show the .aspx and
code-behind files for the application that produces this result.

Figure 1-20. Output of DataGrid allowing selection anywhere
Discussion
To allow selection of a row of data simply by clicking on it, you
create a DataGrid in the usual fashion but add a
hidden ButtonColumn. The
ButtonType attribute is set to
"LinkButton“, and the
CommandName attribute is set to
"Select“. This causes the
DataGrid to be rendered with a hidden hyperlinked
Select button in every row.
<Columns>
<asp:ButtonColumn ButtonType="LinkButton"
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