|
| 1 | +--- |
| 2 | +title: Selection |
| 3 | +page_title: Grid for Blazor | Selection |
| 4 | +description: Enable and configure selection in Grid for Blazor |
| 5 | +slug: components/grid/features/selection |
| 6 | +tags: telerik,blazor,grid,selection |
| 7 | +published: True |
| 8 | +position: 22 |
| 9 | +--- |
| 10 | + |
| 11 | +# Grid Selection |
| 12 | + |
| 13 | +The Grid component offers support for row selection. |
| 14 | + |
| 15 | +You can configure the selection by setting `SelectionMode` to a member of the `Telerik.Blazor.GridSelectionMode` enum. The row selection can be: |
| 16 | + |
| 17 | +* `Single` |
| 18 | +* `Multiple` |
| 19 | + |
| 20 | +You can configure initial selected items by setting the `SelectedItems` property to a collection of items from the Grid's `Data`. You could either use two-way binding, or `SelectedItemsChanged` event to track user selection. |
| 21 | + |
| 22 | +The Grid supports selection via a checkbox column to be used. To define it, add a `TelerikGridCheckboxColumn` in the `TelerikGridColumns` collection of the grid. |
| 23 | + |
| 24 | +## Single Selection |
| 25 | + |
| 26 | +In Single SelectionMode, selection is applied via a click on a row, or clicking a checkbox if `TelerikGridCheckboxColumn` is configured. |
| 27 | + |
| 28 | +>caption Single Selection and SelectedItemsChanged usage |
| 29 | +
|
| 30 | +````CSHTML |
| 31 | +@using Telerik.Blazor.Components.Grid |
| 32 | +@if (SelectedEmployee != null) |
| 33 | +{ |
| 34 | + <span>Selected Employee: @SelectedEmployee.Name </span> |
| 35 | +} |
| 36 | +<TelerikGrid Data=@GridData |
| 37 | + SelectionMode="GridSelectionMode.Single" |
| 38 | + SelectedItemsChanged="@((IEnumerable<Employee> employeeList) => OnSelect(employeeList))" Height="400px"> |
| 39 | + <TelerikGridColumns> |
| 40 | + <TelerikGridColumn Field=@nameof(Employee.Name) /> |
| 41 | + <TelerikGridColumn Field=@nameof(Employee.Team) Title="Team" /> |
| 42 | + <TelerikGridColumn Field=@nameof(Employee.IsOnLeave) Title="On Vacation" /> |
| 43 | + </TelerikGridColumns> |
| 44 | +</TelerikGrid> |
| 45 | +
|
| 46 | +@code { |
| 47 | + public List<Employee> GridData { get; set; } |
| 48 | + public Employee SelectedEmployee { get; set; } |
| 49 | +
|
| 50 | + protected override void OnInitialized() |
| 51 | + { |
| 52 | + GridData = new List<Employee>(); |
| 53 | + var rand = new Random(); |
| 54 | + for (int i = 0; i < 15; i++) |
| 55 | + { |
| 56 | + GridData.Add(new Employee() |
| 57 | + { |
| 58 | + EmployeeId = i, |
| 59 | + Name = "Employee " + i.ToString(), |
| 60 | + Team = "Team " + i % 3, |
| 61 | + IsOnLeave = i % 2 == 0 |
| 62 | + }); |
| 63 | + } |
| 64 | + } |
| 65 | +
|
| 66 | + protected void OnSelect(IEnumerable<Employee> employees) |
| 67 | + { |
| 68 | + SelectedEmployee = employees.FirstOrDefault(); |
| 69 | + } |
| 70 | +
|
| 71 | + public class Employee |
| 72 | + { |
| 73 | + public int EmployeeId { get; set; } |
| 74 | + public string Name { get; set; } |
| 75 | + public string Team { get; set; } |
| 76 | + public bool IsOnLeave { get; set; } |
| 77 | + } |
| 78 | +} |
| 79 | +
|
| 80 | +```` |
| 81 | + |
| 82 | +## Multiple Selection |
| 83 | + |
| 84 | +In Multiple SelectionMode, selection could be made using the following approaches: |
| 85 | + |
| 86 | +* Select the checkbox of each desired row, or |
| 87 | +* Press and hold `Ctrl`, and click the desired rows |
| 88 | + |
| 89 | +For range selection, you could use the `Shift` key and click the row you want to be the last in the range. As a result, all rows between the initially selected row and the last one are selected. |
| 90 | + |
| 91 | +>caption Multiple Selection and SelectedItems two-way binding |
| 92 | +
|
| 93 | +````CSHTML |
| 94 | +@using Telerik.Blazor.Components.Grid |
| 95 | +
|
| 96 | +<h3> |
| 97 | + Selected Employees: |
| 98 | +</h3> |
| 99 | +<ul> |
| 100 | + @foreach (Employee employee in Employees.ToList()) |
| 101 | + { |
| 102 | + <li> |
| 103 | + @employee.Name |
| 104 | + </li> |
| 105 | + } |
| 106 | +</ul> |
| 107 | +<TelerikGrid Data=@GridData |
| 108 | + SelectionMode="GridSelectionMode.Multiple" |
| 109 | + @bind-SelectedItems="@Employees" Height="400px"> |
| 110 | + <TelerikGridColumns> |
| 111 | + <TelerikGridColumn Field=@nameof(Employee.Name) /> |
| 112 | + <TelerikGridColumn Field=@nameof(Employee.Team) Title="Team" /> |
| 113 | + <TelerikGridColumn Field=@nameof(Employee.IsOnLeave) Title="On Vacation" /> |
| 114 | + </TelerikGridColumns> |
| 115 | +</TelerikGrid> |
| 116 | +
|
| 117 | +@code { |
| 118 | + public List<Employee> GridData { get; set; } |
| 119 | + public IEnumerable<Employee> Employees { get; set; } |
| 120 | +
|
| 121 | + protected override void OnInitialized() |
| 122 | + { |
| 123 | + GridData = new List<Employee>(); |
| 124 | + var rand = new Random(); |
| 125 | + for (int i = 0; i < 15; i++) |
| 126 | + { |
| 127 | + GridData.Add(new Employee() |
| 128 | + { |
| 129 | + EmployeeId = i, |
| 130 | + Name = "Employee " + i.ToString(), |
| 131 | + Team = "Team " + i % 3, |
| 132 | + IsOnLeave = i % 2 == 0 |
| 133 | + }); |
| 134 | + } |
| 135 | +
|
| 136 | + Employees = GridData.Take(2); |
| 137 | + } |
| 138 | +
|
| 139 | + public class Employee |
| 140 | + { |
| 141 | + public int EmployeeId { get; set; } |
| 142 | + public string Name { get; set; } |
| 143 | + public string Team { get; set; } |
| 144 | + public bool IsOnLeave { get; set; } |
| 145 | + } |
| 146 | +} |
| 147 | +```` |
| 148 | + |
| 149 | +## Checkbox Support |
| 150 | + |
| 151 | +Configure `TelerikGridCheckboxColumn` to render a checkbox column to enhance the selection user experience. |
| 152 | + |
| 153 | +The Grid allows selection and deselection of rows on a page via the `SelectAll` property. This property will render a checkbox in the grid header that could be used for selection of whole pages. |
| 154 | + |
| 155 | +>caption Checkbox support |
| 156 | +
|
| 157 | +````CSHTML |
| 158 | +@using Telerik.Blazor.Components.Grid |
| 159 | +<TelerikGrid Data=@GridData |
| 160 | + SelectionMode="GridSelectionMode.Multiple" |
| 161 | + Height="265px" |
| 162 | + Pageable="true" |
| 163 | + PageSize="5"> |
| 164 | + <TelerikGridColumns> |
| 165 | + <TelerikGridCheckboxColumn SelectAll="true"></TelerikGridCheckboxColumn> |
| 166 | + <TelerikGridColumn Field=@nameof(Employee.Name) /> |
| 167 | + <TelerikGridColumn Field=@nameof(Employee.Team) Title="Team" /> |
| 168 | + <TelerikGridColumn Field=@nameof(Employee.IsOnLeave) Title="On Vacation" /> |
| 169 | + </TelerikGridColumns> |
| 170 | +</TelerikGrid> |
| 171 | +
|
| 172 | +@code { |
| 173 | + public List<Employee> GridData { get; set; } |
| 174 | +
|
| 175 | + protected override void OnInitialized() |
| 176 | + { |
| 177 | + GridData = new List<Employee>(); |
| 178 | + var rand = new Random(); |
| 179 | + for (int i = 0; i < 15; i++) |
| 180 | + { |
| 181 | + GridData.Add(new Employee() |
| 182 | + { |
| 183 | + EmployeeId = i, |
| 184 | + Name = "Employee " + i.ToString(), |
| 185 | + Team = "Team " + i % 3, |
| 186 | + IsOnLeave = i % 2 == 0 |
| 187 | + }); |
| 188 | + } |
| 189 | + } |
| 190 | +
|
| 191 | + public class Employee |
| 192 | + { |
| 193 | + public int EmployeeId { get; set; } |
| 194 | + public string Name { get; set; } |
| 195 | + public string Team { get; set; } |
| 196 | + public bool IsOnLeave { get; set; } |
| 197 | + } |
| 198 | +} |
| 199 | +```` |
| 200 | + |
| 201 | +>note Selection in `Incell` EditMode could be applied only via a checkbox column. This is required due to the overlapping action that triggers selection and incell editing. |
| 202 | +
|
| 203 | + |
| 204 | +## See Also |
| 205 | + |
| 206 | + * [Live Demo: Grid Selection](https://demos.telerik.com/blazor-ui/grid/selection) |
| 207 | + |
0 commit comments