This documentation is archived and is not being maintained.

Allowing Users to Select Items in a DataGrid Web Server Control

Visual Studio .NET 2003

You can set up your DataGrid so that a user can select a particular row by clicking a button. When a row is selected, the SelectedIndex property of the DataGrid is set to the index of the selected row, and the row is displayed using the Selected Item format. There are three ways to create a button control that has "Select" as the CommandName property. You can:

  • Have the Property Builder create the Select button
  • Create the button yourself in the Property Builder
  • Add the button to a template column

To have the Property Builder create a Select button

  1. Add a DataGrid to your Web Forms page. For details, see Adding DataGrid Web Server Controls to a Web Forms Page.
  2. In Design view, select the DataGrid control, then click the Property Builder link at the bottom of the Properties window.
  3. In the DataGrid Properties dialog box, click the Columns tab.
  4. In the Available columns box, expand the Button Column item and choose Select.
  5. Click the Add button (Aa984289.vxadd(en-us,VS.71).gif) to add the Select column to the Selected columns list.
  6. The Command name property for the button is Select. You must leave this property set to Select if you want the row to display the Selected Item format when the user clicks the button.
  7. Set the rest of the buttons as you want. For information about setting properties for Button Columns, see Adding Button Columns to a DataGrid Web Server Control.
  8. When the grid is displayed, a column with buttons labeled "Select" will appear. When the user clicks the select button, the corresponding row will be formatted using the selected item format. For more information see, Specifying Grid Item Format in a DataGrid Web Server Control.

In the previous steps, the Property Builder created a button that would allow users to select a row. In the next procedure, you use the Property Builder to create a button, but you specify the properties so that the button implements the selection behavior.

To create a Select button using the Property Builder

  1. Add a DataGrid to your Web Forms page. For more information, see Adding DataGrid Web Server Controls to a Web Forms Page.
  2. In Design view, select the DataGrid control, then click the Property Builder link at the bottom of the Properties window.
  3. In the DataGrid Properties dialog box, click the Columns tab.
  4. In the Available columns box, click Button Column.
  5. Click the Add (Aa984289.vxadd(en-us,VS.71).gif) button to add the Button column to the Selected Columns list.
  6. Set the Command name property to Select.
  7. Set the rest of the buttons as you want. For details about setting properties for button columns, see Adding Button Columns to a DataGrid Web Server Control.
  8. When the grid is displayed, a column with buttons labeled Select appears. When the user clicks the select button, the corresponding row will be formatted using the selected item format. For more information see, Specifying Grid Item Format in a DataGrid Web Server Control.

To add a Select button to a template column

  1. Add a template column to your DataGrid. For details, see Adding Template Columns to a DataGrid Web Server Control.
  2. In Design view, right-click the DataGrid, point to Edit Template, and choose the template to which you want to add a Select button.
  3. Add a Button, ImageButton, or LinkButton control to the ItemTemplate.
  4. Set the CommandName property of the button control to Select.
  5. When the grid is displayed, a column with buttons labeled "Select" will appear. When the user clicks the select button, the corresponding row will be formatted using the selected item format. For details see Specifying Grid Item Format in a DataGrid Web Server Control.

See Also

DataGrid Columns | Adding DataGrid Web Server Controls to a Web Forms Page | DataGrid Web Server Control | Introduction to the DataGrid Web Server Control | Allowing Users to Edit Items in a DataGrid Web Server Control | Allowing Users to Delete Items in a DataGrid Web Server Control

Show: