How to preserve selected rows while navigating between pages in MAUI DataGrid?
When navigating between pages in a .NET MAUI DataGrid, the disposal of the DataGrid can lead to the loss of selected rows. This article demonstrates a workaround to maintain the selected rows while navigating between pages.
Step 1: Initialize the SfDataGrid with the necessary properties. Set up an ObservableCollection in the ViewModel class to manage the selected rows.
Step 2: Implement the ObservableCollection in the ViewModel class to handle selected rows. Use the SfDataGrid.SelectionChanged event in the corresponding DataGrid page class to capture selected rows. Upon the event triggering, load the selected rows into the local ObservableCollection collection.
Step 3: Use the SfDataGrid.SelectedRows property to programmatically select rows by assigning the locally stored selected rows when opening the DataGrid Page.
public partial class EmployeePage : ContentPage
{
public EmployeePage(EmployeeViewModel employeeViewModel)
{
InitializeComponent();
this.BindingContext = employeeViewModel;
if (employeeViewModel.EmployeeSelectedRow != null)
{
dataGrid.SelectedRows = new ObservableCollection<object>(employeeViewModel.EmployeeSelectedRow);
}
dataGrid.SelectionChanged += dataGrid_SelectionChanged;
}
private void dataGrid_SelectionChanged(object sender, Syncfusion.Maui.DataGrid.DataGridSelectionChangedEventArgs e)
{
EmployeeViewModel employeeViewModel = this.BindingContext as EmployeeViewModel;
employeeViewModel.EmployeeSelectedRow = new ObservableCollection<object>(dataGrid.SelectedRows);
}
}
Download the complete sample from GitHub.
Conclusion
I hope you enjoyed learning how to preserve selected rows while navigating between pages in MAUI DataGrid.
You can refer to our .NET MAUI DataGrid feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started with configuration specifications. Explore our .NET MAUI DataGrid example to understand how to create and manipulate data.
For current customers, check out our components from the License and Downloads page. If you are new to Syncfusion®, try our 30-day free trial to check out our other controls.
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. We are always happy to assist you!