diff --git a/src/Framework/Core/Controls/Options/DistanceNearPageIndexesProvider.cs b/src/Framework/Core/Controls/Options/DistanceNearPageIndexesProvider.cs
index f40ac13687..79cdec24d9 100644
--- a/src/Framework/Core/Controls/Options/DistanceNearPageIndexesProvider.cs
+++ b/src/Framework/Core/Controls/Options/DistanceNearPageIndexesProvider.cs
@@ -26,8 +26,10 @@ public DistanceNearPageIndexesProvider(int distance)
/// The settings for paging.
public IList GetIndexes(IPagingOptions pagingOptions)
{
- var firstIndex = Math.Max(pagingOptions.PageIndex - distance, 0);
- var lastIndex = Math.Min(pagingOptions.PageIndex + distance + 1, pagingOptions.PagesCount);
+ var count = pagingOptions.PagesCount;
+ var index = Math.Max(0, Math.Min(count - 1, pagingOptions.PageIndex)); // clamp index to be a valid page
+ var firstIndex = Math.Max(index - distance, 0);
+ var lastIndex = Math.Min(index + distance + 1, count);
return Enumerable
.Range(firstIndex, lastIndex - firstIndex)