Serialization and Deserialization in WPF DataGrid (SfDataGrid)

SfDataGrid allows you to serialize and deserialize the SfDataGrid settings using DataContractSerializer.

Serialization

You can serialize the SfDataGrid by using SfDataGrid.Serialize method which exports the current DataGrid control properties to an XML file.

using (var file = File.Create("DataGrid.xml"))
{           
    dataGrid.Serialize(file);
}    

Serialize as Stream

You can store the SfDataGrid settings as Stream using Serialize method by passing the stream.

FileStream stream = new FileStream("DataGrid", FileMode.Create);
this.dataGrid.Serialize(stream);

Serialization options

SfDataGrid serialization operation can be customized by passing SerializationOptions instance as an argument to Serialize method.

Serialize sorting

By default, SfDataGrid allows you to serialize the sorting operation. You can disable the sorting serialization by setting the SerializationOptions.SerializeSorting to false.

using (var file = File.Create("DataGrid.xml"))
{
    SerializationOptions options = new SerializationOptions();
    options.SerializeSorting = false;
    dataGrid.Serialize(file, options);
}

Serialize grouping

By default, SfDataGrid allows you to serialize the grouping operation. You can disable the grouping serialization by setting the SerializationOptions.SerializeGrouping to false.

using (var file = File.Create("DataGrid.xml"))
{
    SerializationOptions options = new SerializationOptions();
    options.SerializeGrouping = false;
    dataGrid.Serialize(file, options);
}

Serialize filtering

By default, SfDataGrid allows you to serialize the filtering operation. You can disable the filtering serialization by setting the SerializationOptions.SerializeFiltering to false.

using (var file = File.Create("DataGrid.xml"))
{
    SerializationOptions options = new SerializationOptions();
    options.SerializeFiltering = false;
    dataGrid.Serialize(file, options);
}

Serialize columns

By default, SfDataGrid allows you to serialize the columns manipulation operation. You can disable the columns serialization by setting the SerializationOptions.SerializeColumns to false.

using (var file = File.Create("DataGrid.xml"))
{
    SerializationOptions options = new SerializationOptions();
    options.SerializeColumns = false;
    dataGrid.Serialize(file, options);
}

Serialize summaries

By default, SfDataGrid allows you to serialize the caption summary, group summary and table summary settings in SfDataGrid.

You can disable the summaries serialization by setting SerializationOptions.SerializeCaptionSummary , SerializationOptions.SerializeGroupSummaries , SerializationOptions.SerializeTableSummaries properties to false

using (var file = File.Create("DataGrid.xml"))
{
    SerializationOptions options = new SerializationOptions();
    options.SerializeCaptionSummary = false;
    options.SerializeGroupSummaries = false;
    options.SerializeTableSummaries = false;
    dataGrid.Serialize(file, options);
}

Serialize stacked headers

By default, SfDataGrid allows you to serialize the stack headers operation. You can disable the stack headers serialization by setting the SerializationOptions.SerializeStackedHeaders to false.

using (var file = File.Create("DataGrid.xml"))
{
    SerializationOptions options = new SerializationOptions();
    options.SerializeStackHeaders = false;
    dataGrid.Serialize(file, options);
}

Serialize Details View

By default, SfDataGrid allows you to serialize the DetailsViewDefinition. You can disable the DetailsViewDefinition serialization by setting the SerializationOptions.SerializeDetailsViewDefinition to false.

using (var file = File.Create("DataGrid.xml"))
{
    SerializationOptions options = new SerializationOptions();
    options.SerializeDetailsViewDefinition = false;
    dataGrid.Serialize(file, options);
}

Serialize unbound rows

By default, SfDataGrid allows you to serialize the unbound rows settings. You can disable the unbound rows serialization by setting the SerializationOptions.SerializeUnBoundRows to false.

using (var file = File.Create("DataGrid.xml"))
{
    SerializationOptions options = new SerializationOptions();
    options.SerializeUnBoundRows = false;
    dataGrid.Serialize(file, options);
}

Deserialization

You can deserialize the SfDataGrid setting by using SfDataGrid.Deserialize method which reconstructs the SfDataGrid based on the setting in the stored XML file.

using (var file = File.Open("DataGrid.xml", FileMode.Open))
{
    dataGrid.Deserialize(file);
}    

Deserialize from Stream

You can deserialize the SfDataGrid settings from Stream using Deserialize method.

FileStream fileStream = new FileStream("DataGrid", FileMode.Open);
this.dataGrid.Deserialize(fileStream);

Deserialization options

Deserialization operation can be customized by passing DeserializationOptions instance as an argument to Deserialize method.

Deserialize sorting

By default, SfDataGrid allows you to deserialize the sorting operation. You can disable the sorting deserialization by setting the DeserializationOptions.DeserializeSorting to false.

using (var file = File.Open("DataGrid.xml", FileMode.Open))
{
    DeserializationOptions options = new DeserializationOptions();
    options.DeserializeSorting = false ;
    dataGrid.Deserialize(file, options);
}

Deserialize grouping

By default, SfDataGrid allows you to deserialize the grouping operation. You can disable the grouping deserialization by setting the DeserializationOptions.DeserializeGrouping to false.

using (var file = File.Open("DataGrid.xml", FileMode.Open))
{
    DeserializationOptions options = new DeserializationOptions();
    options.DeserializeGrouping = false ;
    dataGrid.Deserialize(file, options);
}

Deserialize filtering

By default, SfDataGrid allows you to deserialize the filtering. you can disable the filtering deserialization by setting DeserializationOptions.DeserializeFiltering to false.

using (var file = File.Open("DataGrid.xml", FileMode.Open))
{
    DeserializationOptions options = new DeserializationOptions();
    options.DeserializeFiltering = false ;
    dataGrid.Deserialize(file, options);
}

Deserialize columns

By default, SfDataGrid allows you to deserialize the columns manipulation operations. You can disable the columns deserialization by setting the DeserializationOptions.DeserializeColumns to false.

using (var file = File.Open("DataGrid.xml", FileMode.Open))
{
    DeserializationOptions options = new DeserializationOptions();
    options.DeserializeColumns = false ;
    dataGrid.Deserialize(file, options);
}

Deserialize summaries

By default, SfDataGrid allows you to deserialize the group summary, caption summary and table summary settings.

You can disable the summaries deserialization by setting DeserializationOptions.DeserializeCaptionSummary , DeserializationOptions.DeserializeGroupSummaries , DeserializationOptions.DeserializeTableSummaries properties to false.

using (var file = File.Open("DataGrid.xml", FileMode.Open))
{
    DeserializationOptions options = new DeserializationOptions();
    options.DeserializeCaptionSummary = false ;
    options.DeserializeGroupSummaries = false ;
    options.DeserializeTableSummaries = false ;
    dataGrid.Deserialize(file, options);
}    

Deserialize stacked headers

By default, SfDataGrid allows you to deserialize the stack headers. You can disable the stacked headers deserialization by setting the DeserializationOptions.DeserializeStackedHeaders to false.

using (var file = File.Open("DataGrid.xml", FileMode.Open))
{
    DeserializationOptions options = new DeserializationOptions();
    options.DeserializeStackedHeaders = false ;
    dataGrid.Deserialize(file, options);
}

Deserialize Details View

By default, SfDataGrid allows you to deserialize the DetailsViewDefinition. You can disable the DetailsViewDefinition deserialization by setting the DeserializationOptions.DeserializeDetailsViewDefinition to false.

using (var file = File.Open("DataGrid.xml", FileMode.Open))
{
    DeserializationOptions options = new DeserializationOptions();
    options.DeserializeDetailsViewDefinition = false ;
    dataGrid.Deserialize(file, options);
}

Deserialize unbound rows

By default, SfDataGrid allows you to deserialize the unbound rows settings. You can disable the unbound rows deserialization by setting the DeserializationOptions.DeserializeUnBoundRows to false.

using (var file = File.Open("DataGrid.xml", FileMode.Open))
{
    DeserializationOptions options = new DeserializationOptions();
    options.DeserializeUnBoundRows = false ;
    dataGrid.Deserialize(file, options);
}

Customizing Serialization and Deserialization Operations

SfDataGrid allows you to customize the serialization and deserialization operations by deriving SerializationController class and override the necessary virtual methods.

Serialize custom column

By default, the unknown(custom) column types are serialized as GridTextColumn type. If you want to serialize the custom column, you have to add custom column type into predefined types.

In the below code snippet, DatePickerColumn is created . For more information about creating custom column refer here.

public class DatePickerColumn : GridColumn
{        
    public DatePickerColumn()
    {
        SetCellType("DatePicker");
    }

    public static readonly DependencyProperty DateMappingNameProperty = DependencyProperty.Register("DateMappingName",
typeof(string), typeof(DatePickerColumn));

    public string DateMappingName
    {
        get { return (string)GetValue(DateMappingNameProperty); }
        set { SetValue(DateMappingNameProperty, value); }
    }

    protected override Freezable CreateInstanceCore()
    {
        return new DatePickerColumn();
    }

    protected override void SetDisplayBindingConverter()
    {
        (this.DisplayBinding as Binding).Converter = new CustomConverter();      
    }
}

In the below code snippet, the DatePickerColumn is defined in SfDataGrid.

<syncfusion:SfDataGrid x:Name="dataGrid"
                        AutoGenerateColumns="False"
                        ItemsSource="{Binding Orders}">

    <syncfusion:SfDataGrid.Columns>
        <local:DatePickerColumn HeaderText="OrderDate" MappingName="OrderDate" />
    </syncfusion:SfDataGrid.Columns>

</syncfusion:SfDataGrid>

To serialize the above DatePickerColumn, follow the below steps.

  1. Create a class derived from SerializableGridColumn and define the custom column properties in SerializableCustomGridColumn class.
[DataContract(Name="SerializableCustomGridColumn")]

public class SerializableCustomGridColumn : SerializableGridColumn
{
    [DataMember]

    public string DateMappingName { get; set; }
}

2.Create a new class named as SerializationControllerExt by overriding SerializationController class.

dataGrid.SerializationController = new SerializationControllerExt(dataGrid);

public class SerializationControllerExt : SerializationController
{
 
    public SerializationControllerExt(SfDataGrid dataGrid)
        : base(dataGrid)
    {
    }
}

3.You can get the custom column property settings for serialization by overriding the GetSerializableGridColumn virtual method.

public class SerializationControllerExt : SerializationController
{

    public SerializationControllerExt(SfDataGrid dataGrid)
        : base(dataGrid)
    {
    }

    protected override SerializableGridColumn GetSerializableGridColumn(GridColumn column)
    {

        if (column.MappingName == "OrderDate")
        {
            return new SerializableCustomGridColumn();
        }
        return base.GetSerializableGridColumn(column);
    }
}

4.Store the custom column property settings during serialization by overriding the StoreGridColumnProperties virtual method.

public class SerializationControllerExt : SerializationController
{

    public SerializationControllerExt(SfDataGrid dataGrid)
            : base(dataGrid)
    {
    }

    protected override void StoreGridColumnProperties(GridColumn column, SerializableGridColumn serializableColumn)
    {
        base.StoreGridColumnProperties(column, serializableColumn);

        if (column is DatePickerColumn)
            (serializableColumn as SerializableCustomGridColumn).DateMappingName = (column as DatePickerColumn).DateMappingName;
    }
}

5.Add the custom column in to known column types by overriding the KnownTypes virtual method.

public class SerializationControllerExt : SerializationController
{
    public SerializationControllerExt(SfDataGrid dataGrid)
            : base(dataGrid)
    {
    }

    public override Type[] KnownTypes()
    {
        var types = base.KnownTypes();
        var list = types.Cast<Type>().ToList();
        list.Add(typeof(SerializableCustomGridColumn));
        return list.ToArray();
    }
}

6.During deserialization, you can get the custom column settings from SerializableGridColumn by overriding GetGridColumn virtual method.

public class SerializationControllerExt : SerializationController
{
    public SerializationControllerExt(SfDataGrid dataGrid)
            : base(dataGrid)
    {
    }

    protected override GridColumn GetGridColumn(SerializableGridColumn serializableColumn)
    {

        if (serializableColumn is SerializableCustomGridColumn)
            return new DatePickerColumn();
            return base.GetGridColumn(serializableColumn);
    }
}

7.Now restore the custom column settings from SerializableGridColumn by overriding the RestoreColumnProperties virtual method.

public class SerializationControllerExt : SerializationController
{

    public SerializationControllerExt(SfDataGrid dataGrid)
        : base(dataGrid)
    {
    }

    protected override void RestoreColumnProperties(SerializableGridColumn serializableColumn, GridColumn column)
    {
        base.RestoreColumnProperties(serializableColumn, column);
        
        if (column is DatePickerColumn)
            (column as DatePickerColumn).DateMappingName = (serializableColumn as SerializableCustomGridColumn).DateMappingName;
    }

}

You can download the sample demo here.

Serializing template column content

By default, you cannot serialize the template content in SfDataGrid. This is the default behavior during Serialization and Deserialization operation.

<Application.Resources>
    <DataTemplate x:Key="cellTemplate">
        <Button Content="{Binding Value}" />
    </DataTemplate>
</Application.Resources>

<syncfusion:SfDataGrid x:Name="dataGrid"
                       AutoGenerateColumns="False"
                       ItemsSource="{Binding Orders}">

    <syncfusion:SfDataGrid.Columns>

        <syncfusion:GridTemplateColumn CellTemplate="{StaticResource cellTemplate}"
                                       HeaderText="Order ID"
                                       MappingName="OrderID"
                                       SetCellBoundValue="True" />        
    </syncfusion:SfDataGrid.Columns>

</syncfusion:SfDataGrid>

If you want to serialize and deserialize the template content , you have to reconstruct the same template during deserialization in RestoreColumnProperties method.

this.dataGrid.SerializationController = new SerializationControllerExt(this.dataGrid);

public class SerializationControllerExt : SerializationController
{

    public SerializationControllerExt(SfDataGrid grid)
        : base(grid)
    {

    }
        
    protected override void RestoreColumnProperties(SerializableGridColumn serializableColumn, GridColumn column)
    {
        base.RestoreColumnProperties(serializableColumn, column);

        if (column is GridTemplateColumn)
        {

            if (column.MappingName == "OrderID")
            {
                column.CellTemplate = App.Current.Resources["cellTemplate"] as DataTemplate;
            }
        }
    }
}

You can download the sample demo here.