|
| 1 | +--- |
| 2 | +title: Set "All" PageSize Option as Default |
| 3 | +description: How to set the "All" option for the PageSize as default in Telerik Grid for Blazor. |
| 4 | +type: how-to |
| 5 | +page_title: How to Set "All" PageSize Option as Default |
| 6 | +slug: grid-kb-set-all-pagesize-option-as-default |
| 7 | +tags: grid, page, size, all, option, default |
| 8 | +ticketid: 1624891 |
| 9 | +res_type: kb |
| 10 | +--- |
| 11 | + |
| 12 | +## Environment |
| 13 | +<table> |
| 14 | +<tbody> |
| 15 | +<tr> |
| 16 | +<td>Product</td> |
| 17 | +<td>Grid for Blazor</td> |
| 18 | +</tr> |
| 19 | +</tbody> |
| 20 | +</table> |
| 21 | + |
| 22 | +## Description |
| 23 | + |
| 24 | +I want to set the default selected entry in the Pager of the Telerik Grid to be "All" items. However, the `PageSize` property only accepts a non-nullable `int`, so I cannot bind it to a nullable `int?` variable. Is there a way to initialize the `PageSize` with `null` to indicate that I want the "All" option as the default? |
| 25 | + |
| 26 | +## Solution |
| 27 | + |
| 28 | +The `PageSize` parameter of the Grid accepts a non-nullable `int` by design. To set the "All" option as the default for the page size in the Telerik Grid, use a custom approach as follows: |
| 29 | + |
| 30 | +1. In the component's initialization method (`OnInitializedAsync`), load the data and set the `PageSize` property to the total count of the records. |
| 31 | +2. Add the "All" option (`null`) to the `PageSizes` collection. |
| 32 | +3. Bind the `PageSize` property to the `@bind-PageSize` attribute of the `TelerikGrid` component. |
| 33 | +4. Specify the `PageSizes` collection in the `GridPagerSettings` component to display the dropdown with the available page sizes. |
| 34 | + |
| 35 | +>caption Set "All" PageSize Option as Default |
| 36 | +
|
| 37 | +``` |
| 38 | +Page size: @PageSize |
| 39 | +
|
| 40 | +<TelerikGrid Data="@GridData" |
| 41 | + Pageable="true" |
| 42 | + @bind-PageSize="@PageSize" |
| 43 | + Height="500px"> |
| 44 | + <GridSettings> |
| 45 | + <GridPagerSettings InputType="PagerInputType.Buttons" |
| 46 | + PageSizes="@PageSizes" |
| 47 | + ButtonCount="5"> |
| 48 | + </GridPagerSettings> |
| 49 | + </GridSettings> |
| 50 | + <GridColumns> |
| 51 | + <GridColumn Field="@(nameof(Product.Name))" Title="Product Name" /> |
| 52 | + <GridColumn Field="@(nameof(Product.Price))" /> |
| 53 | + <GridColumn Field="@(nameof(Product.Released))" /> |
| 54 | + <GridColumn Field="@(nameof(Product.Discontinued))" /> |
| 55 | + </GridColumns> |
| 56 | +</TelerikGrid> |
| 57 | +
|
| 58 | +@code { |
| 59 | + private List<Product> GridData { get; set; } = new List<Product>(); |
| 60 | +
|
| 61 | + private int PageSize { get; set; } |
| 62 | +
|
| 63 | + private List<int?> PageSizes { get; set; } = new List<int?> { null, 5, 10, 15 }; |
| 64 | +
|
| 65 | + protected override async Task OnInitializedAsync() |
| 66 | + { |
| 67 | + await LoadData(); |
| 68 | +
|
| 69 | + PageSize = GridData.Count(); |
| 70 | + } |
| 71 | +
|
| 72 | + private async Task LoadData() |
| 73 | + { |
| 74 | + GridData = Enumerable.Range(1, 30).Select(x => new Product |
| 75 | + { |
| 76 | + Id = x, |
| 77 | + Name = "Product name " + x, |
| 78 | + Price = (decimal)(x * 3.14), |
| 79 | + Released = DateTime.Now.AddMonths(-x).Date, |
| 80 | + Discontinued = x % 5 == 0 |
| 81 | + }).ToList(); |
| 82 | + } |
| 83 | +
|
| 84 | + public class Product |
| 85 | + { |
| 86 | + public int Id { get; set; } |
| 87 | + public string Name { get; set; } |
| 88 | + public decimal Price { get; set; } |
| 89 | + public DateTime Released { get; set; } |
| 90 | + public bool Discontinued { get; set; } |
| 91 | + } |
| 92 | +} |
| 93 | +``` |
0 commit comments