GridView Selection Features

Other usage samples: Simple GridView | AutoGenerated Columns | Template Fields | BindAttribute | Row Selection | Paging | Sorting | Inline Editing | Selection | Display Properties |

This sample demonstrates the built-in selection support using AutoGenerateSelectButton, SelectedIndex, SelectedRowStyle, and the SelectedIndexChanging event.


Auto-Generated Select Button

Set AutoGenerateSelectButton="true" to render a Select link in each row. The SelectedRowStyle highlights the chosen row.

IDProductCategoryPrice 
1WidgetTools9.99SelectEdit
2GadgetElectronics19.99SelectEdit
3SprocketHardware4.50SelectEdit
4GizmoElectronics29.99SelectEdit
5DoohickeyTools14.75SelectEdit

Selected index: -1  

Selection changes: 0


Code Example

<GridView ItemType="Product"
          Items="@_products"
          AutoGenerateSelectButton="true"
          SelectedIndex="@_selectedIndex"
          SelectedIndexChanging="HandleSelecting"
          SelectedIndexChanged="HandleSelected">
    <SelectedRowStyleContent>
        <GridViewSelectedRowStyle BackColor="WebColor.Yellow" Font_Bold="true" />
    </SelectedRowStyleContent>
    <Columns>
        <BoundField DataField="Name" HeaderText="Product" />
    </Columns>
</GridView>

@code {
    private int _selectedIndex = -1;

    void HandleSelecting(GridViewSelectEventArgs e)
    {
        // e.NewSelectedIndex, e.Cancel
        _selectedIndex = e.NewSelectedIndex;
    }

    void HandleSelected(int index)
    {
        // index contains the newly selected row
    }
}