aspxtreme

Allowing Users to Select Items in a DataGrid Control

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


The DataGrid control can be set to allow users to select individual items in the list.

This is enabled basically by providing a command-type button control in the ItemTemplate of a DataGrid's TemplateColumn that, essentially, serves to select the item. The button control can be an <asp:Button>, <asp:LinkButton> or <asp:ImageButton> bound to a field in the data source.

NOTE: The buttons do not automatically select the item. Instead, each button raises an ItemCommand event when clicked, which typically is used to trigger a handler that sets the item associated with the event into selected mode.

The DataGrid also encapsulates the functionality to easily highlight the selected item visually, using the SelectedItemStyle property.

Unlike the DataList control, though, the DataGrid does not support a SelectedItemTemplate in the TemplateColumn type. As such, the DataGrid cannot be set to display additional text and controls for the SelectedItem, that may be different from, or not included in, the ItemTemplate.

The following examples demonstrate different ways of enabling users to select an item from a DataGrid.

DataGrid SelectedItem Property
Run Sample | View Source
DataGrid SelectedIndex Property
Run Sample | View Source
DataGrid SelectedIndexChanged Event
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