When navigating between pages, the DataGrid gets disposed, resulting in the loss of selection. This article demonstrates a workaround to maintain the selected rows while navigating between pages in a .NET MAUI DataGrid.
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. Utilize 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);
}
}