diff --git a/Maui.DataGrid/DataGrid.xaml.cs b/Maui.DataGrid/DataGrid.xaml.cs index 293245c..3c036b3 100644 --- a/Maui.DataGrid/DataGrid.xaml.cs +++ b/Maui.DataGrid/DataGrid.xaml.cs @@ -215,6 +215,8 @@ private void SortAndPaginate(SortData? sortData = null) var originalItems = ItemsSource.Cast().ToList(); + PageCount = (int)Math.Ceiling(originalItems.Count / (double)PageSize); + IList sortedItems; if (sortData != null && CanSort(sortData)) @@ -404,20 +406,14 @@ private void SortAndPaginate(SortData? sortData = null) oldCollection.CollectionChanged -= self.OnItemsSourceCollectionChanged; } - self.InternalItems = n?.Cast().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) { @@ -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().Count() / (double)self.PageSize); - } self.SortAndPaginate(); self.UpdatePageSizeList(); } @@ -682,7 +674,7 @@ private void SortAndPaginate(SortData? sortData = null) }, (b, o, n) => { - if (o != n && b is DataGrid self && self.ItemsSource?.Cast().Any() == true) + if (o != n && b is DataGrid self) { self.SortAndPaginate(); }