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.
| ID | Product | Category | Price | |
|---|---|---|---|---|
| 1 | Widget | Tools | 9.99 | SelectEdit |
| 2 | Gadget | Electronics | 19.99 | SelectEdit |
| 3 | Sprocket | Hardware | 4.50 | SelectEdit |
| 4 | Gizmo | Electronics | 29.99 | SelectEdit |
| 5 | Doohickey | Tools | 14.75 | SelectEdit |
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
}
}