Articles in this section
Category / Section

How to maintain or serialize, de-serialize the column order and width when you navigate page in WinRT Grid?

2 mins read

How to maintain or serialize, de-serialize the column order and width when you navigate page?

You can maintain the SfDataGrid settings like column order, column width, sorting, grouping and etc. when navigating between page by using serialization and deserialization feature.

You can save the SfDataGrid settings in the file by using the SfDataGrid.Serialize method when you navigate to other pages. You can customize the serialization operation by setting SerializationOptions.

C#:

protected async override void OnNavigatedFrom(NavigationEventArgs e)
        {
            var folder = KnownFolders.DocumentsLibrary;
            try
            {
                var storageFile = await folder.CreateFileAsync("DataGrid.xml", CreationCollisionOption.ReplaceExisting);
                var options = new SerializationOptions();
                options.SerializeColumns = true;
                this.SfdataGrid.Serialize(storageFile, options);
            }
            catch (Exception)
            {
            }
            base.OnNavigatedFrom(e);
        }

 

Then you can de-serialize the SfDataGrid setting from the saved file by using SfDataGrid.Deserialize method when you navigate to the same page again. You can customize the de-serialization operation by setting DeserializationOptions.

 

 

 

 

 

C#:

protected async override void OnNavigatedTo(NavigationEventArgs e)
        {
            //De-serializing the DataGrid when moving to current page
            var folder = KnownFolders.DocumentsLibrary;
            try
            {
                var storageFile = await folder.GetFileAsync("DataGrid.xml");
                if (storageFile != null)
                {
                    var options = new DeserializationOptions();
                    options.DeserializeColumns = true;
                    this.SfdataGrid.Deserialize(storageFile, options);
                    await storageFile.DeleteAsync();
                }
            }
            catch (Exception)
            {
            }
            base.OnNavigatedTo(e);
        }

 

Note:

The above code snippet is applicable only for WinRT platform. For WPF, you can use the same method to maintain the SfDataGrid settings when closing and opening the application.


Samples

WPF

WRT

UWP

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