aspxtreme

Allowing Users to Select Rows in a GridView Control

Controls You Can Use on Web Forms   ASP.NET Data Controls   GridView Control


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:

  1. 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;
    GridView AutoGenerateSelectButton Example
    Run Sample | View Source
  2. 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.

    GridView SelectedRow Example
    Run Sample | View Source
    Dynamically Generated ButtonField ImageButtons
    Run Sample | View Source
    GridView SelectedDataKey Example
    Run Sample | View Source

Now let's take a closer look at how all these are done.


Books and more ...


Suggested Reading

Need a break ?


More ...
Back to top

Check out related books at Amazon

© 2000-2008 Rey Nuñez All rights reserved.

If you have any question, comment or suggestion
about this site, please send us a note

You can help support aspxtreme