Articles in this section
Category / Section

How to maintain the column order, width, sorting, grouping in WPF DataGrid?

4 mins read

In WPF DataGrid (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 DataGrid settings in the file by using the Serialize method when you navigate to other pages. You can customize the serialization operation by setting SerializationOptions.

protected async override void OnNavigatedFrom(NavigationEventArgs e)
{
    // Serializing the DataGrid when moving to the current page
    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 DataGrid setting from the saved file by using Deserialize method when you navigate to the same page again. You can customize the de-serialization operation by setting DeserializationOptions.
protected async override void OnNavigatedTo(NavigationEventArgs e)
{
    // De-serializing the DataGrid when moving to the 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);
}
Sample Links:

WPF

WRT

UWP 


Conclusion

I hope you enjoyed learning about how to maintain the column order, width, sorting, grouping in DataGrid.

You can refer to our WPF DataGrid feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our WPF DataGrid example to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!
Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied