Articles in this section
Category / Section

How to save and reload the filters in WinUI DataGrid (SfDataGrid)?

1 min read

WinUI DataGrid (SfDataGrid) allows to save and load the grid settings using the SfDataGrid.Serialize and SfDataGrid.Deserialize methods. This process can be customized by passing SerializationOptions and DeserializationOptions as parameters to these methods. SerializationOptions and DeserializationOptions contains a set of properties that is used to customize the serialization/deserialization process.

Filters applied to the DataGrid can be saved by enabling the serialization of filters by setting SerializationOptions.SerializeFiltering property as illustrated in the following code example.

private void OnSerializeClicked(object sender, RoutedEventArgs e)
{
            if (dataGrid == null) return;
            var serializationOptions = new SerializationOptions()
            {
                SerializeFiltering = true,
            };
            using (var file = File.Create("DataGrid.xml"))
            {
                dataGrid.Serialize(file, serializationOptions);
            }
}

In the same way, the deserialization of serialized filters can be performed by enabling DeserializationOptions.DeserializeFiltering property as illustrated in the following code example.

private void OnDeserializeClicked(object sender, RoutedEventArgs e)
{
            if (dataGrid == null) return;
            var deserializationOptions = new DeserializationOptions()
            {
                DeserializeFiltering = true,
            };
            using (var file = File.Open("DataGrid.xml", FileMode.Open))
            {
                if (deserializationOptions.DeserializeFiltering == true)
                {
                    dataGrid.Deserialize(file, deserializationOptions);
                }
            }
}

Serialization and Deserialization applied in SfDataGridTake a moment to peruse the WinUI DataGrid - Serialization and Deserialization documentation, where you can find about serialization and deserialization with code examples.

View Sample in GitHub

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