|
| 1 | +--- |
| 2 | +title: Add a New Row and Navigate to the Last Page |
| 3 | +description: Learn how to add a new row to the Grid for Blazor using an external button and navigate to the last page where the new row is inserted. |
| 4 | +type: how-to |
| 5 | +page_title: Programmatically Add New Rows and Navigate to the Last Page in Telerik Blazor Grid |
| 6 | +slug: grid-add-new-row-navigate-last-page |
| 7 | +tags: grid, blazor, add, row, navigate, page, programmatically, last |
| 8 | +res_type: kb |
| 9 | +ticketid: 1667656 |
| 10 | +--- |
| 11 | + |
| 12 | +## Environment |
| 13 | + |
| 14 | +<table> |
| 15 | + <tbody> |
| 16 | + <tr> |
| 17 | + <td>Product</td> |
| 18 | + <td>Grid for Blazor</td> |
| 19 | + </tr> |
| 20 | + </tbody> |
| 21 | +</table> |
| 22 | + |
| 23 | +## Description |
| 24 | + |
| 25 | +I want to programmatically add a new row to the Grid at the end of the data and navigate to the last page to view the added row. |
| 26 | + |
| 27 | +## Solution |
| 28 | + |
| 29 | +To add a new row at the end of the Grid and navigate to the last page: |
| 30 | +1. Add an external button that triggers the addition of a new item to your data collection. |
| 31 | +2. Calculate the page number required to display the newly added item (based on the total number of items and the page size). |
| 32 | +3. Programmatically set the Grid's page to the calculated page number to navigate to the last page. |
| 33 | + |
| 34 | +Below is an example demonstrating this approach: |
| 35 | + |
| 36 | +````CSHTML |
| 37 | +@* Add/remove employee to see how the Grid reacts to that change. *@ |
| 38 | +
|
| 39 | +<TelerikButton OnClick="@AddEmployee">Add employee</TelerikButton> |
| 40 | +
|
| 41 | +<TelerikButton OnClick="@RemoveEmployee">Remove last employee</TelerikButton> |
| 42 | +
|
| 43 | +<TelerikGrid Data="@MyData" |
| 44 | + Height="400px" |
| 45 | + Pageable="true" |
| 46 | + Sortable="true" |
| 47 | + Page="@currentPage" |
| 48 | + PageSize="@pageSize"> |
| 49 | + <GridColumns> |
| 50 | + <GridColumn Field="@(nameof(SampleData.Id))" Width="120px" /> |
| 51 | + <GridColumn Field="@(nameof(SampleData.Name))" Title="Employee Name" /> |
| 52 | + <GridColumn Field="@(nameof(SampleData.Team))" Title="Team" /> |
| 53 | + <GridColumn Field="@(nameof(SampleData.HireDate))" Title="Hire Date" /> |
| 54 | + </GridColumns> |
| 55 | +</TelerikGrid> |
| 56 | +
|
| 57 | +@code { |
| 58 | + private int currentPage = 1; |
| 59 | + private int pageSize = 10; |
| 60 | +
|
| 61 | + private void AddEmployee() |
| 62 | + { |
| 63 | + var x = MyData.Count + 1; |
| 64 | + MyData.Add(new SampleData |
| 65 | + { |
| 66 | + Id = x, |
| 67 | + Name = "name " + x, |
| 68 | + Team = "team " + x % 5, |
| 69 | + HireDate = DateTime.Now.AddDays(-x).Date |
| 70 | + }); |
| 71 | + MyData = new List<SampleData>(MyData); |
| 72 | +
|
| 73 | + currentPage = (int)Math.Ceiling((double)MyData.Count / pageSize); |
| 74 | + } |
| 75 | +
|
| 76 | + private void RemoveEmployee() |
| 77 | + { |
| 78 | + if (MyData.Count > 0) |
| 79 | + { |
| 80 | + MyData.RemoveAt(MyData.Count - 1); |
| 81 | + MyData = new List<SampleData>(MyData); |
| 82 | + } |
| 83 | + } |
| 84 | +
|
| 85 | + private List<SampleData> MyData = Enumerable.Range(1, 5).Select(x => new SampleData |
| 86 | + { |
| 87 | + Id = x, |
| 88 | + Name = "name " + x, |
| 89 | + Team = "team " + x % 5, |
| 90 | + HireDate = DateTime.Now.AddDays(-x).Date |
| 91 | + }).ToList(); |
| 92 | +
|
| 93 | + public class SampleData |
| 94 | + { |
| 95 | + public int Id { get; set; } |
| 96 | + public string Name { get; set; } |
| 97 | + public string Team { get; set; } |
| 98 | + public DateTime HireDate { get; set; } |
| 99 | + } |
| 100 | +} |
| 101 | +```` |
| 102 | + |
| 103 | +## See Also |
| 104 | + |
| 105 | +- [Grid Overview](https://docs.telerik.com/blazor-ui/components/grid/overview) |
| 106 | +- [Grid Paging](https://docs.telerik.com/blazor-ui/components/grid/paging) |
0 commit comments