Skip to content

Commit

Permalink
set PageCount only inside pagination routine
Browse files Browse the repository at this point in the history
  • Loading branch information
Edward Miller committed Dec 29, 2023
1 parent 518610f commit 4336672
Showing 1 changed file with 7 additions and 15 deletions.
22 changes: 7 additions & 15 deletions Maui.DataGrid/DataGrid.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,8 @@ private void SortAndPaginate(SortData? sortData = null)

var originalItems = ItemsSource.Cast<object>().ToList();

PageCount = (int)Math.Ceiling(originalItems.Count / (double)PageSize);

IList<object> sortedItems;

if (sortData != null && CanSort(sortData))
Expand Down Expand Up @@ -404,20 +406,14 @@ private void SortAndPaginate(SortData? sortData = null)
oldCollection.CollectionChanged -= self.OnItemsSourceCollectionChanged;
}

self.InternalItems = n?.Cast<object>().ToList();

// Subscribe to new collection's change event and update properties
if (self.InternalItems != null)
if (n is INotifyCollectionChanged newCollection)
{
if (n is INotifyCollectionChanged newCollection)
{
newCollection.CollectionChanged += self.OnItemsSourceCollectionChanged;
}

self.PageCount = (int)Math.Ceiling(self.InternalItems.Count / (double)self.PageSize);
self.SortAndPaginate();
newCollection.CollectionChanged += self.OnItemsSourceCollectionChanged;
}

self.SortAndPaginate();

// Reset SelectedItem if it's not in the new collection
if (self.SelectedItem != null && self.InternalItems?.Contains(self.SelectedItem) != true)
{
Expand Down Expand Up @@ -455,10 +451,6 @@ private void SortAndPaginate(SortData? sortData = null)
if (o != n && b is DataGrid self)
{
self.PageNumber = 1;
if (self.ItemsSource != null)
{
self.PageCount = (int)Math.Ceiling(self.ItemsSource.Cast<object>().Count() / (double)self.PageSize);
}
self.SortAndPaginate();
self.UpdatePageSizeList();
}
Expand Down Expand Up @@ -682,7 +674,7 @@ private void SortAndPaginate(SortData? sortData = null)
},
(b, o, n) =>
{
if (o != n && b is DataGrid self && self.ItemsSource?.Cast<object>().Any() == true)
if (o != n && b is DataGrid self)
{
self.SortAndPaginate();
}
Expand Down

0 comments on commit 4336672

Please sign in to comment.