Skip to content

Commit

Permalink
store the old sorted column
Browse files Browse the repository at this point in the history
  • Loading branch information
Edward Miller committed Dec 27, 2023
1 parent 15a678e commit 13e0429
Showing 1 changed file with 12 additions and 15 deletions.
27 changes: 12 additions & 15 deletions Maui.DataGrid/DataGrid.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,11 @@ private void OnItemsSourceCollectionChanged(object? sender, NotifyCollectionChan
{
if (o != n && b is DataGrid self)
{
if (n != null)
{
self._sortedColumn = self.Columns[n.Index];
}

self.SortAndPaginate(n);
}
});
Expand Down Expand Up @@ -683,6 +688,7 @@ public IEnumerable ItemsSource
}

private IList<object>? _internalItems;
private DataGridColumn? _sortedColumn;

internal IList<object>? InternalItems
{
Expand Down Expand Up @@ -1020,30 +1026,21 @@ private void OnSelectionChanged(object? sender, SelectionChangedEventArgs e)
oldColumns?.ForEach(oldColumn => oldColumn.SizeChanged -= OnColumnSizeChanged);
newColumns?.ForEach(newColumn => newColumn.SizeChanged += OnColumnSizeChanged);

DataGridColumn? oldSortedColumn = null;

if (oldColumns != null && SortedColumnIndex != null)
{
oldSortedColumn = oldColumns[SortedColumnIndex.Index];
}

int? newSortedColumnIndex = null;

if (newColumns != null && oldSortedColumn != null)
if (newColumns != null && _sortedColumn != null)
{
newSortedColumnIndex = newColumns.IndexOf(oldSortedColumn);
newSortedColumnIndex = newColumns.IndexOf(_sortedColumn);
}

if (oldSortedColumn != null && SortedColumnIndex != null)
if (_sortedColumn != null && SortedColumnIndex != null)
{
if (newSortedColumnIndex == null)
if (newSortedColumnIndex is null or -1)
{
return null;
}
else
{
return new(newSortedColumnIndex.Value, SortedColumnIndex.Order);
}

return new(newSortedColumnIndex.Value, SortedColumnIndex.Order);
}

return SortedColumnIndex;
Expand Down

0 comments on commit 13e0429

Please sign in to comment.