How to persist data in the SFDataGrid when device changes orientation

Hello

I have this grid filled with data. The code is simple:

           grid = new SfDataGrid(this);

            grid.AllowResizingColumn = true;
            grid.AllowSorting = true;
    
            layout.AddView(grid);

            string dpPath = Path.Combine("/data/data/TPProject.TPProject/files/", "PranfHurto.db3");

            var db = new SQLiteConnection(dpPath);

            grid.ItemsSource = db.Table<v_Casos>(); //This is SQLite

However if the device change it's orientation, the OnCreate method is called again, basically needing to re-query the table, and this takes times. I need to save the state of the ItemSources in the grid, so I don't need to query the table again. In this post of Xamarin, they explain how to save the activity state, however db.Table<v_Casos>(); returns a list, and I'm not sure how to save a whole list of v_Casos type inside the Outstate.

Any thoughts about how to achieve this?? Thank you!

3 Replies

SO Samuel Otero December 21, 2017 12:09 PM UTC

Following this Xamarin suggestion I think I'm able to persist the data if I serialize the object to a Json string. However something is wrong. My "savedInstanceState.GetString("jsonData")" always comes back null because OnSaveInstanceState never gets called!!

Here is my code:

protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            SetContentView(Resource.Layout.CaseControl);
            RelativeLayout layout = FindViewById(Resource.Id.layoutCasos);

            grid = new SfDataGrid(this);

            grid.AllowResizingColumn = true;
            grid.AllowSorting = true;
    
            layout.AddView(grid);

            if (savedInstanceState != null)
            {
               string savedJsonData = savedInstanceState.GetString("jsonData");
               grid.ItemsSource = JsonConvert.DeserializeObject>(savedJsonData);

            }

            else
            {
                string dpPath = Path.Combine("/data/data/TPProject.TPProject/files/", "PranfHurto.db3");
                var db = new SQLiteConnection(dpPath);
                var data = db.Table();
                grid.ItemsSource = data;

                jsonData = JsonConvert.SerializeObject(data);
            }

            grid.SelectionMode = SelectionMode.Single;
            grid.GridLongPressed += RowLongPress;
            
        }

        protected override void OnSaveInstanceState(Bundle outState)
        {
            outState.PutString("jsonData", jsonData);

            base.OnSaveInstanceState(outState);
        }

So, what I'm doing wrong?? Help! Thank you!


SO Samuel Otero December 21, 2017 12:40 PM UTC

I read in another post a similar problem and the weird solution was re copy and pasting the overriden method. Now it started to work. Thank you.


AN Ashok N Syncfusion Team December 21, 2017 05:09 PM UTC

Hi Samuel, 

Thanks for your update. Please let us know if you require further assistance on this.  

Regards, 
Ashok 


Loader.
Up arrow icon