How to refresh sorting when items source collection changed dynamically in .NET MAUI DataGrid SfDataGrid?
This article explains how to refresh sorting when the bound collection is updated dynamically in the Syncfusion .NET MAUI DataGrid.
When setting sorting on a column using SortColumnDescription, set the LiveDataUpdateMode property to AllowDataShaping to reapply the sorting when the underlying collection is modified dynamically.
Xaml
<ContentPage.BindingContext>
<local:OrderInfoRepository x:Name="viewModel" />
</ContentPage.BindingContext>
<Grid RowDefinitions="50,*">
<Button Text="ChangeCollection"
Clicked="Button_Clicked" />
<syncfusion:SfDataGrid x:Name="dataGrid" Grid.Row="1" LiveDataUpdateMode="AllowDataShaping"
ItemsSource="{Binding OrderInfoCollection}">
<syncfusion:SfDataGrid.Columns>
<syncfusion:DataGridTextColumn MappingName="OrderID"
HeaderText="Order ID" />
</syncfusion:SfDataGrid.Columns>
<syncfusion:SfDataGrid.SortColumnDescriptions>
<syncfusion:SortColumnDescription ColumnName="OrderID"
SortDirection="Descending" />
</syncfusion:SfDataGrid.SortColumnDescriptions>
</syncfusion:SfDataGrid>
</Grid>
Xaml.cs
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
}
private void Button_Clicked(object sender, EventArgs e)
{
viewModel.OrderInfoCollection.Clear();
viewModel.ChangeCollection();
}
}
OrderInfoRepository.cs
public void ChangeCollection()
{
orderInfo.Add(new OrderInfo("2001", "Selena Alvarad", "Sweden", "BERGS", "London"));
orderInfo.Add(new OrderInfo("2002", "Sandra Altamirano", "Mexico", "ANATR", "Mexico D.F."));
orderInfo.Add(new OrderInfo("2003", "Oscar Alpuerto", "France", "BLONP", "Strasbourg"));
orderInfo.Add(new OrderInfo("2004", "Cecil Allison", "UK", "AROUT", "London"));
orderInfo.Add(new OrderInfo("2005", "Tim Adams", "Germany", "ALFKI", "Berlin"));
orderInfo.Add(new OrderInfo("2006", "Michael Allen", "Canada", "BOTTM", "Lenny Lin"));
orderInfo.Add(new OrderInfo("2007", "Andrew Fuller", "France", "BLONP", "Strasbourg"));
orderInfo.Add(new OrderInfo("2008", "Martin King", "Spain", "BOLID", "Madrid"));
orderInfo.Add(new OrderInfo("2009", "Phyllis Allen", "France", "BONAP", "Marsiella"));
orderInfo.Add(new OrderInfo("2010", "John Carter", "UK", "AROUT", "London"));
orderInfo.Add(new OrderInfo("2011", "Marvin Allen", "Sweden", "BERGS", "London"));
orderInfo.Add(new OrderInfo("2012", "Anne Wilson", "Germany", "BLAUS", "Mannheim"));
orderInfo.Add(new OrderInfo("2013", "Sean Jacobson", "Mexico", "ANTON", "Mexico D.F."));
orderInfo.Add(new OrderInfo("2014", "Gina Irene", "UK", "AROUT", "London"));
}
Executing the code example above yields the following output:
View sample in GitHub
Take a moment to explore this documentation, where you can find more information about Syncfusion .NET MAUI DataGrid (SfDataGrid) with code examples. Please refer to this link to learn about the essential features of Syncfusion .NET MAUI DataGrid (SfDataGrid).
Conclusion
I hope you enjoyed learning about how to refresh the sorting when the collection changed dynamically in SfDataGrid.
You can refer to our .NET MAUI DataGrid’s feature tour page to learn about its other groundbreaking feature representations. You can also explore our .NET MAUI DataGrid Documentation to understand how to present and manipulate data. For current customers, you can check out our .NET MAUI components on the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to explore our .NET MAUI DataGrid and other .NET MAUI components.
If you have any queries or require clarifications, please let us know in the comments below. You can also contact us through our support forums,Direct-Trac or feedback portal, or the feedback portal. We are always happy to assist you!