How to retain the SfDataGrid properties when changing the data source in .NET MAUI?
The .NET MAUI DataGrid supports retaining properties such as sorting and grouping when the data source is changed by setting the SfDataGrid.AutoGenerateColumnsMode to an option other than ResetAll. This allows the grid to maintain column settings when the ItemsSource is updated.
The SfDataGrid.AutoGenerateColumnsMode includes the following modes,
- None
- Reset
- ResetAll
- RetainOld
- SmartReset
XAML
In this example, AutoGenerateColumnsMode is set to None, which retains only the columns explicitly defined in the SfDataGrid.Columns collection, preserving sorting and grouping settings when the ItemsSource changes.
<syncfusion:SfDataGrid x:Name="datagrid"
ItemsSource="{Binding Employees}"
AutoGenerateColumnsMode="None"
DefaultColumnWidth="155">
<syncfusion:SfDataGrid.Columns>
<syncfusion:DataGridTextColumn MappingName="EmployeeID"
HeaderText="Employee ID" />
<syncfusion:DataGridTextColumn MappingName="Name"
HeaderText="Name" />
<syncfusion:DataGridTextColumn MappingName="IDNumber"
HeaderText="ID Number" />
</syncfusion:SfDataGrid.Columns>
</syncfusion:SfDataGrid>
Download the complete sample from GitHub
Conclusion
I hope you enjoyed learning how to retain the SfDataGrid properties when changing the data source in .NET MAUI.
You can refer to our .NET MAUI DataGrid feature tour page to learn about its other groundbreaking feature representations. Explore our .NET MAUI DataGrid Documentation to understand how to present and manipulate data.
For current customers, check out our .NET MAUI components on the License and Downloads page. If you are new to Syncfusion®, try our 30-day free trial to explore our .NET MAUI DataGrid and other .NET MAUI components.
Please let us know in the comments section if you have any queries or require clarification. 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!