Articles in this section
Category / Section

How to retain the SfDataGrid properties when changing the data source?

2 mins read

SfDataGrid have a support for retaining the old values such as sorting and grouping when reloading the SfDataGrid. It can be achieved by setting SfDataGrid.AutoGenerateColumnsMode as other than ResetAll.

When you change ItemsSource for SfDataGrid, then the columns are generated on the basis of option set for SfDataGrid.AutoGenerateColumnsMode.

None:

  • Stores only the columns that are defined in SfDataGrid.Columns collection.
  • When changing the ItemsSource, the grouping and sorting for explicitly defined SfDataGrid.Columns alone will be retained.
    public partial class MainPage : ContentPage
    {
            private SfDataGrid dataGrid;
            private ViewModel viewModel;
            public MainPage()
            {
                InitializeComponent();
                dataGrid = new SfDataGrid();
                viewModel = new ViewModel();
                dataGrid.ItemsSource = viewModel.OrdersInfo;
                dataGrid.AutoGenerateColumnsMode = AutoGenerateColumnsMode.None;
                this.Content = dataGrid;
            }
    }
    

 

Reset:

  • Retains the columns defined explicitly in the application level and creates columns newly for all the other properties in a data source.
  • When changing the ItemsSource, the grouping and sorting for explicitly defined SfDataGrid.Columns alone will be retained.
    public partial class MainPage : ContentPage
    {
            private SfDataGrid dataGrid;
            private ViewModel viewModel;
            public MainPage()
            {
                InitializeComponent();
                dataGrid = new SfDataGrid();
                viewModel = new ViewModel();
                dataGrid.ItemsSource = viewModel.OrdersInfo;
                dataGrid.AutoGenerateColumnsMode = AutoGenerateColumnsMode.Reset;
                this.Content = dataGrid;
            }
    }
    

 

RetainOld:

  • When changing the ItemsSource, creates columns for all fields in a data source when the Grid does not have any explicit definition for columns.
  • When columns are defined explicitly, then the defined columns alone are retained and new columns are not created.
  • Similarly, when changing the ItemsSource and when the Grid have any explicit definition for columns, the grouping and sorting are retained as it is.

public partial class MainPage : ContentPage

{

        private SfDataGrid dataGrid;

        private ViewModel viewModel;

        public MainPage()

        {

            InitializeComponent();

            dataGrid = new SfDataGrid();

            viewModel = new ViewModel();

            dataGrid.ItemsSource = viewModel.OrdersInfo;

            dataGrid.AutoGenerateColumnsMode = AutoGenerateColumnsMode.RetainOld;

            this.Content = dataGrid;

        }

}

 

SmartReset:

  • Retains the columns defined explicitly in application level and the columns with MappingName identical to the properties in the new data source.
  • Creates columns newly for all the other properties in the data source.
  • Similarly, it retains the grouping and sorting of the columns that are defined explicitly in application level and the columns with MappingName identical to the properties in new data source.
    public partial class MainPage : ContentPage
    {
            private SfDataGrid dataGrid;
            private ViewModel viewModel;
            public MainPage()
            {
                InitializeComponent();
                dataGrid = new SfDataGrid();
                viewModel = new ViewModel();
                dataGrid.ItemsSource = viewModel.OrdersInfo;
                dataGrid.AutoGenerateColumnsMode = AutoGenerateColumnsMode.SmartReset;
                this.Content = dataGrid;
            }
    }
    

 

ResetAll:

  • When changing the ItemsSource, the columns for the previous data source are cleared and the columns will be created newly for the new data source.
  • Even when columns are explicitly defined it does not consider the defined columns and creates the column based on the underlying collection.
  • Further, when changing the ItemsSource, the grouping and sorting for all the columns will be cleared.
    public partial class MainPage : ContentPage
    {
            private SfDataGrid dataGrid;
            private ViewModel viewModel;
            public MainPage()
            {
                InitializeComponent();
                dataGrid = new SfDataGrid();
                viewModel = new ViewModel();
                dataGrid.ItemsSource = viewModel.OrdersInfo;
                dataGrid.AutoGenerateColumnsMode = AutoGenerateColumnsMode.ResetAll;
                this.Content = dataGrid;
            }
    }
    

 

Sample Link:

How to retain old values of SfDataGrid when reloading/re-binding SfDataGrid?

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied