Hello,
Recently, improvements were made on the grid paging that ensure data will be visible when the data source changes (see more here).
Here's a sample snippet to test this with, and a short video shows the behavior with the current latest (2.15.0) which is correct - there is always data that is seen even when the new data has fewer items than the original.
@using System.Collections.ObjectModel
<div class="d-flex flex-row my-2">
<TelerikButton OnClick="@AddForecast">Add Item</TelerikButton>
<TelerikButton OnClick="@RemoveForecast">Remove Item</TelerikButton>
<TelerikButton OnClick="@ChangeData">Change Collection</TelerikButton>
</div>
<TelerikGrid Data=@GridData Height="400px" Pageable="true">
<GridColumns>
<GridColumn Field="Id" />
<GridColumn Field="Name" />
</GridColumns>
</TelerikGrid>
@code {
public ObservableCollection<SampleData> GridData { get; set; }
protected override async Task OnInitializedAsync()
{
await LoadData();
}
private async Task LoadData()
{
var forecasts = Enumerable.Range(1, 45).Select(x => new SampleData { Id = x, Name = $"Name {x}" });
GridData = new ObservableCollection<SampleData>(forecasts);
}
void AddForecast()
{
GridData.Add(new SampleData { Id = GridData.Count + 1, Name = $"name {GridData.Count + 1}" });
}
void RemoveForecast()
{
if (GridData.Count > 0)
{
GridData.RemoveAt(0);
}
}
void ChangeData()
{
var forecasts = Enumerable.Range(1, 15).Select(x => new SampleData { Id = x, Name = $"Second collection - Name {x}" });
GridData = new ObservableCollection<SampleData>(forecasts);
}
public class SampleData
{
public int Id { get; set; }
public string Name { get; set; }
}
}
If you want to change this behavior further, you can use the Page parameter of the grid to set the desired page (e.g., always reset it to 1 when you change the data source). You can find a similar example of binding the parameter in the second snippet in the docs here.
Regards,
Marin Bratanov
Progress Telerik