|
| 1 | +--- |
| 2 | +title: Multiple Rows |
| 3 | +page_title: Grid for Blazor | Multiple Selection |
| 4 | +description: Single row selection in the Grid for Blazor |
| 5 | +slug: components/grid/selection/multiple |
| 6 | +tags: telerik,blazor,grid,selection,multiple |
| 7 | +published: True |
| 8 | +position: 2 |
| 9 | +--- |
| 10 | + |
| 11 | +# Multiple Row Selection |
| 12 | + |
| 13 | +The Grid component can allow the user to select many rows at a time, or to select [only one]({%slug components/grid/selection/single %}). |
| 14 | + |
| 15 | +To use **multiple** row selection, set the `SelectionMode` property to `Telerik.Blazor.GridSelectionMode.Multiple`. |
| 16 | + |
| 17 | +In Multiple SelectionMode, selection could be made using the following approaches: |
| 18 | + |
| 19 | +* Click on a row to select only that row (and deselect any others) |
| 20 | +* Press and hold `Ctrl` and click the desired rows to select or deselect them. |
| 21 | +* Click on the starting row of a range of rows you want selected, press and hold `Shift`, and click on the last row of the range. The last selected item is the start point of the range and the current target row is the end of the selection. |
| 22 | +* Select the checkbox of each desired row. |
| 23 | + |
| 24 | +>tip The [Examples](#examples) section showcases how you can use the grid features together. |
| 25 | +
|
| 26 | +## Checkbox Selection |
| 27 | + |
| 28 | +To add checkboxes in each row that the user can use for selection, add a `TelerikGridCheckboxColumn` in the `TelerikGridColumns` collection of the grid. |
| 29 | + |
| 30 | +The Grid allows selection and deselection of all rows on the current page via the `SelectAll` property. Setting this property to `true` (its default value) will render a checkbox in the grid header that the you can click to select whole pages. |
| 31 | + |
| 32 | +## Selected Items |
| 33 | + |
| 34 | +The `SelectedItemsChanged` event receives a collection of the grid data model. It may have no items in it. |
| 35 | + |
| 36 | +You can use the `SelectedItems` collection in two-way binding. You can use this to pre-select rows for your users. |
| 37 | + |
| 38 | +The `SelectedItems` collection persists across paging operations. Changing the page will keep it populated and you can add more items to the selection. |
| 39 | + |
| 40 | +## Examples |
| 41 | + |
| 42 | +### Multiple Row Selection and Checkbox |
| 43 | + |
| 44 | +You can add a checkbox column for selection. It is required if the `InCell` edit mode is used. Otherwise, it is optional. |
| 45 | + |
| 46 | +>caption Multiple Selection and a checkbox column. |
| 47 | +
|
| 48 | +````CSHTML |
| 49 | +@using Telerik.Blazor.Components.Grid |
| 50 | +
|
| 51 | +<TelerikGrid Data=@GridData |
| 52 | + SelectionMode="GridSelectionMode.Multiple" |
| 53 | + Pageable="true"> |
| 54 | + <TelerikGridColumns> |
| 55 | + <TelerikGridCheckboxColumn /> |
| 56 | + <TelerikGridColumn Field=@nameof(Employee.Name) /> |
| 57 | + <TelerikGridColumn Field=@nameof(Employee.Team) Title="Team" /> |
| 58 | + </TelerikGridColumns> |
| 59 | +</TelerikGrid> |
| 60 | +
|
| 61 | +@code { |
| 62 | + public List<Employee> GridData { get; set; } |
| 63 | +
|
| 64 | + protected override void OnInitialized() |
| 65 | + { |
| 66 | + GridData = new List<Employee>(); |
| 67 | + for (int i = 0; i < 15; i++) |
| 68 | + { |
| 69 | + GridData.Add(new Employee() |
| 70 | + { |
| 71 | + EmployeeId = i, |
| 72 | + Name = "Employee " + i.ToString(), |
| 73 | + Team = "Team " + i % 3 |
| 74 | + }); |
| 75 | + } |
| 76 | + } |
| 77 | +
|
| 78 | + public class Employee |
| 79 | + { |
| 80 | + public int EmployeeId { get; set; } |
| 81 | + public string Name { get; set; } |
| 82 | + public string Team { get; set; } |
| 83 | + } |
| 84 | +} |
| 85 | +```` |
| 86 | + |
| 87 | +### SelectedItemsChanged Event |
| 88 | + |
| 89 | +You can respond to the user action of selecting a new item through the `SelectedItemsChanged` event. |
| 90 | + |
| 91 | +>caption Multiple Selection and handling the SelectedItemsChanged event |
| 92 | +
|
| 93 | +````CSHTML |
| 94 | +@using Telerik.Blazor.Components.Grid |
| 95 | +
|
| 96 | +<TelerikGrid Data=@GridData |
| 97 | + SelectionMode="GridSelectionMode.Multiple" |
| 98 | + SelectedItemsChanged="@((IEnumerable<Employee> employeeList) => OnSelect(employeeList))" |
| 99 | + Pageable="true" |
| 100 | + Height="400px"> |
| 101 | + <TelerikGridColumns> |
| 102 | + <TelerikGridCheckboxColumn /> |
| 103 | + <TelerikGridColumn Field=@nameof(Employee.Name) /> |
| 104 | + <TelerikGridColumn Field=@nameof(Employee.Team) Title="Team" /> |
| 105 | + </TelerikGridColumns> |
| 106 | +</TelerikGrid> |
| 107 | +
|
| 108 | +@if (SelectedEmployees != null) |
| 109 | +{ |
| 110 | + <ul> |
| 111 | + @foreach (Employee employee in SelectedEmployees) |
| 112 | + { |
| 113 | + <li> |
| 114 | + @employee.Name |
| 115 | + </li> |
| 116 | + } |
| 117 | + </ul> |
| 118 | +} |
| 119 | +
|
| 120 | +@code { |
| 121 | + public List<Employee> GridData { get; set; } |
| 122 | + public IEnumerable<Employee> SelectedEmployees { get; set; } |
| 123 | +
|
| 124 | + protected override void OnInitialized() |
| 125 | + { |
| 126 | + GridData = new List<Employee>(); |
| 127 | + for (int i = 0; i < 15; i++) |
| 128 | + { |
| 129 | + GridData.Add(new Employee() |
| 130 | + { |
| 131 | + EmployeeId = i, |
| 132 | + Name = "Employee " + i.ToString(), |
| 133 | + Team = "Team " + i % 3 |
| 134 | + }); |
| 135 | + } |
| 136 | + } |
| 137 | +
|
| 138 | + protected void OnSelect(IEnumerable<Employee> employees) |
| 139 | + { |
| 140 | + SelectedEmployees = employees; |
| 141 | + } |
| 142 | +
|
| 143 | + public class Employee |
| 144 | + { |
| 145 | + public int EmployeeId { get; set; } |
| 146 | + public string Name { get; set; } |
| 147 | + public string Team { get; set; } |
| 148 | + } |
| 149 | +} |
| 150 | +```` |
| 151 | + |
| 152 | +@[template](/_contentTemplates/common/general-info.md#event-callback-can-be-async) |
| 153 | + |
| 154 | +@[template](/_contentTemplates/common/issues-and-warnings.md#valuechanged-lambda-required) |
| 155 | + |
| 156 | +### Two-way Binding of SelectedItems |
| 157 | + |
| 158 | +You can predefine the selected items for your users through the two-way binding of the `SelectedItems` property. The collection will be updated by the grid when the selection changes. Note that both binding to the property and using its event cannot be used at the same time, as Blazor only allows one. |
| 159 | + |
| 160 | +>caption Multiple Selection and two-way binding of the `SelectedItems` property |
| 161 | +
|
| 162 | +````CSHTML |
| 163 | +@using Telerik.Blazor.Components.Grid |
| 164 | +
|
| 165 | +<TelerikGrid Data=@GridData |
| 166 | + SelectionMode="GridSelectionMode.Multiple" |
| 167 | + @bind-SelectedItems="SelectedItems" |
| 168 | + Pageable="true" |
| 169 | + Height="400px"> |
| 170 | + <TelerikGridColumns> |
| 171 | + <TelerikGridCheckboxColumn /> |
| 172 | + <TelerikGridColumn Field=@nameof(Employee.Name) /> |
| 173 | + <TelerikGridColumn Field=@nameof(Employee.Team) Title="Team" /> |
| 174 | + </TelerikGridColumns> |
| 175 | +</TelerikGrid> |
| 176 | +
|
| 177 | +@if (SelectedItems != null) |
| 178 | +{ |
| 179 | + <ul> |
| 180 | + @foreach (Employee employee in SelectedItems) |
| 181 | + { |
| 182 | + <li> |
| 183 | + @employee.Name |
| 184 | + </li> |
| 185 | + } |
| 186 | + </ul> |
| 187 | +} |
| 188 | +
|
| 189 | +@code { |
| 190 | + public List<Employee> GridData { get; set; } |
| 191 | + public IEnumerable<Employee> SelectedItems { get; set; } |
| 192 | +
|
| 193 | + protected override void OnInitialized() |
| 194 | + { |
| 195 | + GridData = new List<Employee>(); |
| 196 | + for (int i = 0; i < 15; i++) |
| 197 | + { |
| 198 | + GridData.Add(new Employee() |
| 199 | + { |
| 200 | + EmployeeId = i, |
| 201 | + Name = "Employee " + i.ToString(), |
| 202 | + Team = "Team " + i % 3 |
| 203 | + }); |
| 204 | + } |
| 205 | +
|
| 206 | + // select Employee with 3 through 5 |
| 207 | + SelectedItems = GridData.Skip(2).Take(3).ToList(); |
| 208 | + } |
| 209 | +
|
| 210 | + public class Employee |
| 211 | + { |
| 212 | + public int EmployeeId { get; set; } |
| 213 | + public string Name { get; set; } |
| 214 | + public string Team { get; set; } |
| 215 | + } |
| 216 | +} |
| 217 | +
|
| 218 | +```` |
| 219 | + |
| 220 | + |
| 221 | +## See Also |
| 222 | + |
| 223 | + * [Live Demo: Grid Selection](https://demos.telerik.com/blazor-ui/grid/selection) |
| 224 | + * [Live Demo: Grid Checkbox Selection](https://demos.telerik.com/blazor-ui/grid/checkbox-selection) |
| 225 | + * [Single Selection]({%slug components/grid/selection/single%}}) |
0 commit comments