The GridView control can be set to allow users to select individual rows in the grid.
This is enabled basically by providing a command-type button control for each row that, essentially, serves to select the row. The GridView also encapsulates the functionality to visually highlight the selected row, using the SelectedRowStyle property.
Unlike the DataList control, though, the GridView does not support a SelectedItemTemplate. As such, the GridView cannot be set to display additional text and controls for the SelectedRow, that may be different from, or not included in, the specified row layout.
Providing select controls in the GridView can be done in either of two ways:
- using the automatic selection feature of the grid, in which the select button controls for each row are autogenerated and rendered via a separate CommandField object;
- using customized <asp:Button>, <asp:LinkButton> or <asp:ImageButton> controls that are bound to a field in the data source, and which are rendered either via a ButtonField or in the ItemTemplate of a GridView's TemplateField.
NOTE: In this second scenario, the buttons do not automatically select the row. Instead, each button raises a RowCommand event when clicked, which typically is used to trigger a handler that sets the row associated with the event into selected mode.
Now let's take a closer look at how all these are done. 