We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

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

Platform: Xamarin.Forms |
Control: SfDataGrid |
Product Version: 13.4.0.53
Tags: binding

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?

2X faster development

The ultimate Xamarin UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile