GridView Row Selection Example
Other usage samples:
Simple GridView |
AutoGenerated Columns |
Template Fields |
BindAttribute |
Row Selection |
Paging |
Sorting |
Inline Editing |
Selection |
Display Properties |
This example demonstrates how to select a row in the GridView and retrieve the selected key value.
| | ID | Company | First Name | Last Name |
|---|---|---|---|---|
| 1 | Virus | John | Smith | |
| 2 | Boring | Jose | Rodriguez | |
| 3 | Fun Machines | Jason | Ramirez |
Code Example
This sample demonstrates the master-detail pattern using GridView row selection:
<GridView ItemType="Customer"
DataKeyNames="CustomerID"
Items="@_customers">
<Columns>
<TemplateField ItemType="Customer" HeaderText="">
<ItemTemplate Context="customer">
<button type="button" class="btn btn-sm btn-primary" @onclick="@(() => SelectCustomer(customer))">Select</button>
</ItemTemplate>
</TemplateField>
<BoundField DataField="CustomerID" HeaderText="ID" />
<BoundField DataField="CompanyName" HeaderText="Company" />
<BoundField DataField="FirstName" HeaderText="First Name"/>
<BoundField DataField="LastName" HeaderText="Last Name"/>
</Columns>
</GridView>
@code {
private Customer? SelectedCustomer;
private List<Customer> _customers = Customer.GetCustomers(...).ToList();
private void SelectCustomer(Customer customer)
{
SelectedCustomer = customer;
}
}