|
| 1 | +@page "/ControlSamples/GridView/RowSelection" |
| 2 | +@using SharedSampleObjects.Models |
| 3 | +@using BlazorWebFormsComponents.DataBinding |
| 4 | + |
| 5 | +<h2>GridView Row Selection Example</h2> |
| 6 | + |
| 7 | +<Nav /> |
| 8 | + |
| 9 | +<p>This example demonstrates how to select a row in the GridView and retrieve the selected key value.</p> |
| 10 | + |
| 11 | +<GridView ItemType="Customer" |
| 12 | + AutoGenerateColumns="false" |
| 13 | + DataKeyNames="CustomerID" |
| 14 | + Items="@_customers" |
| 15 | + EmptyDataText="No data available"> |
| 16 | + <Columns> |
| 17 | + <TemplateField ItemType="Customer" HeaderText=""> |
| 18 | + <ItemTemplate Context="customer"> |
| 19 | + <button type="button" class="btn btn-sm btn-primary" @onclick="@(() => SelectCustomer(customer))">Select</button> |
| 20 | + </ItemTemplate> |
| 21 | + </TemplateField> |
| 22 | + <BoundField ItemType="Customer" DataField="CustomerID" HeaderText="ID" /> |
| 23 | + <BoundField ItemType="Customer" DataField="CompanyName" HeaderText="Company" /> |
| 24 | + <BoundField ItemType="Customer" DataField="FirstName" HeaderText="First Name"/> |
| 25 | + <BoundField ItemType="Customer" DataField="LastName" HeaderText="Last Name"/> |
| 26 | + </Columns> |
| 27 | +</GridView> |
| 28 | + |
| 29 | +@if (SelectedCustomer != null) |
| 30 | +{ |
| 31 | + <div style="margin-top: 20px; padding: 15px; border: 1px solid #ccc; background-color: #f9f9f9;"> |
| 32 | + <h3>Selected Customer Details</h3> |
| 33 | + <p><strong>Customer ID:</strong> @SelectedCustomer.CustomerID</p> |
| 34 | + <p><strong>Company Name:</strong> @SelectedCustomer.CompanyName</p> |
| 35 | + <p><strong>First Name:</strong> @SelectedCustomer.FirstName</p> |
| 36 | + <p><strong>Last Name:</strong> @SelectedCustomer.LastName</p> |
| 37 | + </div> |
| 38 | +} |
| 39 | + |
| 40 | +<hr /> |
| 41 | + |
| 42 | +<h3>Code Example</h3> |
| 43 | +<p>This sample demonstrates the master-detail pattern using GridView row selection:</p> |
| 44 | + |
| 45 | +<pre><code><GridView ItemType="Customer" |
| 46 | + DataKeyNames="CustomerID" |
| 47 | + Items="@@_customers"> |
| 48 | + <Columns> |
| 49 | + <TemplateField ItemType="Customer" HeaderText=""> |
| 50 | + <ItemTemplate Context="customer"> |
| 51 | + <button type="button" class="btn btn-sm btn-primary" @@onclick="@@(() => SelectCustomer(customer))">Select</button> |
| 52 | + </ItemTemplate> |
| 53 | + </TemplateField> |
| 54 | + <BoundField DataField="CustomerID" HeaderText="ID" /> |
| 55 | + <BoundField DataField="CompanyName" HeaderText="Company" /> |
| 56 | + <BoundField DataField="FirstName" HeaderText="First Name"/> |
| 57 | + <BoundField DataField="LastName" HeaderText="Last Name"/> |
| 58 | + </Columns> |
| 59 | +</GridView> |
| 60 | + |
| 61 | +@@code { |
| 62 | + private Customer? SelectedCustomer; |
| 63 | + private List<Customer> _customers = new List<Customer> |
| 64 | + { |
| 65 | + new Customer { CustomerID = 1, FirstName = "John", LastName = "Smith", CompanyName = "Acme Corporation" }, |
| 66 | + new Customer { CustomerID = 2, FirstName = "Jane", LastName = "Doe", CompanyName = "TechStart Inc." }, |
| 67 | + new Customer { CustomerID = 3, FirstName = "Bob", LastName = "Johnson", CompanyName = "Global Solutions" }, |
| 68 | + new Customer { CustomerID = 4, FirstName = "Alice", LastName = "Williams", CompanyName = "Innovation Labs" } |
| 69 | + }; |
| 70 | + |
| 71 | + private void SelectCustomer(Customer customer) |
| 72 | + { |
| 73 | + SelectedCustomer = customer; |
| 74 | + // CustomerID (the DataKey) is available via customer.CustomerID |
| 75 | + } |
| 76 | +}</code></pre> |
| 77 | + |
| 78 | +@code { |
| 79 | + private Customer? SelectedCustomer; |
| 80 | + private List<Customer> _customers = new List<Customer> |
| 81 | + { |
| 82 | + new Customer |
| 83 | + { |
| 84 | + CustomerID = 1, |
| 85 | + FirstName = "John", |
| 86 | + LastName = "Smith", |
| 87 | + CompanyName = "Acme Corporation" |
| 88 | + }, |
| 89 | + new Customer |
| 90 | + { |
| 91 | + CustomerID = 2, |
| 92 | + FirstName = "Jane", |
| 93 | + LastName = "Doe", |
| 94 | + CompanyName = "TechStart Inc." |
| 95 | + }, |
| 96 | + new Customer |
| 97 | + { |
| 98 | + CustomerID = 3, |
| 99 | + FirstName = "Bob", |
| 100 | + LastName = "Johnson", |
| 101 | + CompanyName = "Global Solutions" |
| 102 | + }, |
| 103 | + new Customer |
| 104 | + { |
| 105 | + CustomerID = 4, |
| 106 | + FirstName = "Alice", |
| 107 | + LastName = "Williams", |
| 108 | + CompanyName = "Innovation Labs" |
| 109 | + } |
| 110 | + }; |
| 111 | + |
| 112 | + private void SelectCustomer(Customer customer) |
| 113 | + { |
| 114 | + SelectedCustomer = customer; |
| 115 | + } |
| 116 | +} |
0 commit comments