New to Telerik UI for WinFormsStart a free 30-day trial

Serialize Serialize Custom Aggregates

Updated over 6 months ago

RadPivotGrid can persist its layout together with the applied custom aggregations. In this example we will use the function defined in the custom aggregation article.

The DataContractSerializer class requires the .NET 40 version of the assemblies. The suggested approach is also valid serializing when the custom aggregations functions with the other data providers.

Serialize LocalDataSourceProvider with DataContractSerializer

We've added the DataContract attribute to all classes used by LocalDataSourceProvider. So you can easily serialize it by using a DataContractSerializer. Below you will find out how to create a serializer and use it with RadPivotGrid.

Define Members

C#
    
[DataContract]
public class DataProviderSettings
{
    [DataMember]
    public object[] Aggregates { get; set; }
    
    [DataMember]
    public object[] Filters { get; set; }
    
    [DataMember]
    public object[] Rows { get; set; }
    
    [DataMember]
    public object[] Columns { get; set; }
    
    [DataMember]
    public int AggregatesLevel { get; set; }
    [DataMember]
    public PivotAxis AggregatesPosition { get; set; }
}

The next step is to implement the serializer. When serializing the provider, you have to create an instance of the DataProviderSettings class and set all of the properties. After that you can serialize the instance to a file or a stream. When using DataContractSerializer you have to pass a collection of KnownTypes to the serializer. That's why we've created a new PivotSerializationHelper class which has a static member - KnownTypes. It consits of all types you'll need in order to serialize LocalDataSourceProvider.

Data Provider Implementation

C#
    
public abstract class DataProviderSerializer
{
    public abstract IEnumerable<Type> KnownTypes { get; }
        
    public string Serialize(object context)
    {
        string serialized = string.Empty;
        IDataProvider dataProvider = context as IDataProvider;
        if (dataProvider != null)
        {
            MemoryStream stream = new MemoryStream();
                
            DataProviderSettings settings = new DataProviderSettings()
            {
                Aggregates = dataProvider.Settings.AggregateDescriptions.OfType<object>().ToArray(),
                Filters = dataProvider.Settings.FilterDescriptions.OfType<object>().ToArray(),
                Rows = dataProvider.Settings.RowGroupDescriptions.OfType<object>().ToArray(),
                Columns = dataProvider.Settings.ColumnGroupDescriptions.OfType<object>().ToArray(),
                AggregatesLevel = dataProvider.Settings.AggregatesLevel,
                AggregatesPosition = dataProvider.Settings.AggregatesPosition
            };
            
            DataContractSerializer serializer = new DataContractSerializer(typeof(DataProviderSettings), KnownTypes);
            serializer.WriteObject(stream, settings);
            stream.Position = 0;
            var streamReader = new StreamReader(stream);
            serialized += streamReader.ReadToEnd();
        }
    
        return serialized;
    }
        
    public void Deserialize(object context, string savedValue)
    {
        IDataProvider dataProvider = context as IDataProvider;
        if (dataProvider != null)
        {
            var stream = new MemoryStream();
            var tw = new StreamWriter(stream);
            tw.Write(savedValue);
            tw.Flush();
            stream.Position = 0;
            
            DataContractSerializer serializer = new DataContractSerializer(typeof(DataProviderSettings), KnownTypes);
            var result = serializer.ReadObject(stream);
            
            dataProvider.Settings.AggregateDescriptions.Clear();
            foreach (var aggregateDescription in (result as DataProviderSettings).Aggregates)
            {
                dataProvider.Settings.AggregateDescriptions.Add(aggregateDescription);
            }
            
            dataProvider.Settings.FilterDescriptions.Clear();
            foreach (var filterDescription in (result as DataProviderSettings).Filters)
            {
                dataProvider.Settings.FilterDescriptions.Add(filterDescription);
            }
            
            dataProvider.Settings.RowGroupDescriptions.Clear();
            foreach (var rowDescription in (result as DataProviderSettings).Rows)
            {
                dataProvider.Settings.RowGroupDescriptions.Add(rowDescription);
            }
            
            dataProvider.Settings.ColumnGroupDescriptions.Clear();
            foreach (var columnDescription in (result as DataProviderSettings).Columns)
            {
                dataProvider.Settings.ColumnGroupDescriptions.Add(columnDescription);
            }
    
            dataProvider.Settings.AggregatesPosition = (result as DataProviderSettings).AggregatesPosition;
            dataProvider.Settings.AggregatesLevel = (result as DataProviderSettings).AggregatesLevel;
        }
    }
}

The custom function needs to be added the collection already containing the known types.

C#
public class LocalDataSourceSerializer : DataProviderSerializer
{
    private IEnumerable<Type> myKnownTypes = PivotSerializationHelper.KnownTypes.Concat<Type>(new List<Type>()
    {
        typeof(SqrtSumAggregateFunction)
    });
    public override IEnumerable<Type> KnownTypes
    {
        get
        {
            return myKnownTypes;
        }
    }
}

So the last step is to serialize the provider and deserialize it.

Using the Custom Serializer

C#
private string s = @"..\..\custom.xml";
private void serializeBtnClick(object sender, EventArgs e)
{
    //Serialzie
    this.lastSerializadProvider = serializer.Serialize(this.radPivotGrid1.DataProvider);
    File.WriteAllText(s, this.lastSerializadProvider);
}
private void deserializeBtnClick(object sender, EventArgs e)
{
    //Deserialzie
    this.lastSerializadProvider = File.ReadAllText(s);
    serializer.Deserialize(this.radPivotGrid1.DataProvider, this.lastSerializadProvider);
}

See Also