|
| 1 | +--- |
| 2 | +title: Force a Grid to Refresh |
| 3 | +description: how to force a grid to refresh its data |
| 4 | +type: how-to |
| 5 | +page_title: Refresh Grid Data |
| 6 | +slug: grid-force-refresh |
| 7 | +position: |
| 8 | +tags: |
| 9 | +ticketid: 1448335 |
| 10 | +res_type: kb |
| 11 | +--- |
| 12 | + |
| 13 | +## Environment |
| 14 | +<table> |
| 15 | + <tbody> |
| 16 | + <tr> |
| 17 | + <td>Product</td> |
| 18 | + <td>Grid for Blazor</td> |
| 19 | + </tr> |
| 20 | + </tbody> |
| 21 | +</table> |
| 22 | + |
| 23 | + |
| 24 | +## Description |
| 25 | + |
| 26 | +Is there anyway say I can have a button click event cause a Grid to redraw? |
| 27 | + |
| 28 | +I'm using manual data operations, how do I use an observable collection to refresh the grid? |
| 29 | + |
| 30 | +## Solution |
| 31 | + |
| 32 | +### Automatic operations |
| 33 | + |
| 34 | +For general cases, to change the data that is rendered in the grid from an external source, use an `ObservableCollection`: [https://demos.telerik.com/blazor-ui/grid/observable-data](https://demos.telerik.com/blazor-ui/grid/observable-data). To change the entire data collection, `.Clear()` the collection first to notify the grid that this old data is gone, then create a new one with the new data. |
| 35 | + |
| 36 | +### Manual operations |
| 37 | + |
| 38 | +When using manual operations through the [OnRead event](https://docs.telerik.com/blazor-ui/components/grid/manual-operations), the general pattern is to store the last `DataSourceRequest` so you can repeat it over the new data. Here is an example: |
| 39 | + |
| 40 | +````CSHTML |
| 41 | +@using Telerik.DataSource |
| 42 | +@using Telerik.DataSource.Extensions |
| 43 | +@using System.Collections.ObjectModel |
| 44 | +
|
| 45 | +<TelerikButton OnClick="@ChangeData">Change Data Source</TelerikButton> |
| 46 | +
|
| 47 | +<TelerikGrid Data=@GridData TotalCount=@Total OnRead=@ReadItems |
| 48 | + FilterMode=@GridFilterMode.FilterRow Sortable=true Pageable=true EditMode="@GridEditMode.Inline"> |
| 49 | + <GridColumns> |
| 50 | + <GridColumn Field=@nameof(Employee.ID) /> |
| 51 | + <GridColumn Field=@nameof(Employee.Name) Title="Name" /> |
| 52 | + <GridColumn Field=@nameof(Employee.HireDate) Title="Hire Date" /> |
| 53 | + <GridCommandColumn> |
| 54 | + <GridCommandButton Command="Save" Icon="save" ShowInEdit="true">Update</GridCommandButton> |
| 55 | + <GridCommandButton Command="Edit" Icon="edit">Edit</GridCommandButton> |
| 56 | + <GridCommandButton Command="Delete" Icon="delete">Delete</GridCommandButton> |
| 57 | + <GridCommandButton Command="Cancel" Icon="cancel" ShowInEdit="true">Cancel</GridCommandButton> |
| 58 | + </GridCommandColumn> |
| 59 | + </GridColumns> |
| 60 | + <GridToolBar> |
| 61 | + <GridCommandButton Command="Add" Icon="add">Add Employee</GridCommandButton> |
| 62 | + </GridToolBar> |
| 63 | +</TelerikGrid> |
| 64 | +
|
| 65 | +@code { |
| 66 | + public List<Employee> SourceData { get; set; } |
| 67 | + public int Total { get; set; } = 0; |
| 68 | + public ObservableCollection<Employee> GridData { get; set; } = new ObservableCollection<Employee>(); //prevent null reference errors by intializing the field |
| 69 | + DataSourceRequest LastRequest { get; set; }//store the last request so we can repeat it when changing the data |
| 70 | +
|
| 71 | + async void ChangeData() |
| 72 | + { |
| 73 | + SourceData = GenerateData(DateTime.Now.Millisecond.ToString()); |
| 74 | + await LoadData();//repeat the last request when changing data |
| 75 | + } |
| 76 | +
|
| 77 | + async Task LoadData() |
| 78 | + { |
| 79 | + await Task.Delay(500); //simulate network delay from a real async call |
| 80 | + |
| 81 | + //this example uses the Telerik .ToDataSourceResult() method for brevity, you can call an API here instead |
| 82 | + var datasourceResult = SourceData.ToDataSourceResult(LastRequest); |
| 83 | +
|
| 84 | + //should not be needed, but if the data does not update, try clearing it in order to fire the observable collection event |
| 85 | + //GridData.Clear(); |
| 86 | +
|
| 87 | + //update the grid data as usual |
| 88 | + GridData = new ObservableCollection<Employee>(datasourceResult.Data as IEnumerable<Employee>); |
| 89 | + Total = datasourceResult.Total; |
| 90 | +
|
| 91 | + //tell the UI to update |
| 92 | + StateHasChanged(); |
| 93 | + } |
| 94 | +
|
| 95 | + protected override void OnInitialized() |
| 96 | + { |
| 97 | + SourceData = GenerateData(string.Empty); |
| 98 | + } |
| 99 | +
|
| 100 | + protected async Task ReadItems(GridReadEventArgs args) |
| 101 | + { |
| 102 | + LastRequest = args.Request; |
| 103 | +
|
| 104 | + await LoadData(); |
| 105 | + } |
| 106 | +
|
| 107 | + //This sample implements only reading of the data. To add the rest of the CRUD operations see |
| 108 | + //https://docs.telerik.com/blazor-ui/components/grid/editing/overview |
| 109 | +
|
| 110 | + private List<Employee> GenerateData(string generationIdentifier) |
| 111 | + { |
| 112 | + var result = new List<Employee>(); |
| 113 | + var rand = new Random(); |
| 114 | + for (int i = 0; i < 100; i++) |
| 115 | + { |
| 116 | + result.Add(new Employee() |
| 117 | + { |
| 118 | + ID = i, |
| 119 | + Name = $"Name {i} {generationIdentifier}", |
| 120 | + HireDate = DateTime.Now.Date.AddDays(rand.Next(-20, 20)) |
| 121 | + }); |
| 122 | + } |
| 123 | +
|
| 124 | + return result; |
| 125 | + } |
| 126 | +
|
| 127 | + public class Employee |
| 128 | + { |
| 129 | + public int ID { get; set; } |
| 130 | + public string Name { get; set; } |
| 131 | + public DateTime HireDate { get; set; } |
| 132 | + } |
| 133 | +} |
| 134 | +```` |
0 commit comments