How to: Enable Default Selection in the GridView Web Server Control
The ASP.NET GridView control has a built-in selection capability that allows users to select a row in the grid. Selecting a row in a GridView control does not inherently perform any task. However, by adding selection capability, you can add functionality to the grid that relies on users pointing to a specific row. Typical uses for adding selection capability to the GridView control include:
-
When users select a row, the row is redisplayed with a different look.
-
When users select a row, related data is displayed elsewhere in the page, such as in a DetailsView control.
Procedures
To enable selection using the smart tag
-
In Design view, right-click the GridView control and click Show Smart Tag.
-
In the smart tag panel, select Enable Selection.
To enable default selection using the AutoGenerateSelectButton property
-
Select the GridView control, and in the Properties window, set AutoGenerateSelectButton to true.
-or-
-
In Source view, in the <asp:GridView> element, set the AutoGenerateSelecttButton attribute to true:
<asp:GridView Runat="server" ID="GridView1" AutoGenerateSelectButton="true" />
To customize the look of a row in selection mode
-
Set properties for the SelectedRowStyle.
For example, if you set the BackColor subproperty of the SelectedRowStyle to gray, a selected row is displayed with a gray background.
To customize the command text of the Select button
-
Select the GridView control, and in the Properties window, click the ellipsis (...) button in the Columns box.
The Fields dialog box is displayed.
-
Under Selected fields, click Select.
-
Under CommandField properties, in the SelectText box, enter text for the Select button.
To change the command text of the Select button to an image
-
Select the GridView control, and in the Properties window, click the ellipsis (...) button in the Columns box.
The Fields dialog box is displayed.
-
Under Selected fields, click Select.
-
Under CommandField properties, in the SelectImageUrl box, enter or select the URL of the image to use for the Select button.