Skip to main content

PivotGridOptionsData.CustomObjectConverter Property

Gets or sets a converter used to serialize/deserialize arbitrary objects in a custom manner.

Namespace: DevExpress.XtraPivotGrid

Assembly: DevExpress.PivotGrid.v24.2.Core.dll

Declaration

[Browsable(false)]
public ICustomObjectConverter CustomObjectConverter { get; set; }

Property Value

Type Description
ICustomObjectConverter

An object that implements the ICustomObjectConverter interface representing a converter used to serialize/deserialize arbitrary objects in a custom manner.

Property Paths

You can access this nested property as listed below:

Object Type Path to CustomObjectConverter
PivotGridControl
.OptionsData .CustomObjectConverter

Remarks

If some of the data source field values are custom objects (not numeric or string values), use the CustomObjectConverter property to specify a serializer to be used to process them.

The ASPxPivotGrid requires implementing the serializer when the data source contains custom objects as field values. The XtraPivotGrid can process arbitrary field values without a custom serializer specified. It only requires a serializer for saving/restoring the pivot grid layout or data to/from a file or stream.

To implement a custom serializer, create a class implementing the ICustomObjectConverter interface, implement its ICustomObjectConverter.ToString, ICustomObjectConverter.FromString, ICustomObjectConverter.CanConvert and ICustomObjectConverter.GetType methods, and assign a class instance to the CustomObjectConverter property. To learn more, see ICustomObjectConverter.

Example

The following example shows how to implement a custom serializer. Custom serializers are required when data source field values are custom objects (not numeric or string values). In this example, the data source contains a Sales Person field whose values are Employee objects, exposing the FirstName, LastName and Age properties. The Employeeclass implements the IComparable interface, and overrides the GetHashCode, Equals and ToString methods (required to display and handle custom objects). The custom serializer is represented by the CustomObjectConverter class, which implements the ICustomObjectConverter interface. The ToString and FromString methods are implemented to serialize and deserialize the Employee objects, respectively. A CustomObjectConverter class instance is assigned to the PivotGridOptionsData.CustomObjectConverter property. It is used for serializing Sales Person field values when pivot grid data is saved to a stream and restored back by an end-user via clicking the Save and Load buttons respectively.

using System;
using System.IO;
using System.Windows.Forms;
using DevExpress.Data.PivotGrid;
using DevExpress.Utils.Serializing.Helpers;

namespace XtraPivotGrid_CustomObjectConverter {
    public partial class Form1 : Form {
        MemoryStream stream;
        public Form1() {
            InitializeComponent();
            pivotGridControl1.DataSource = DataHelper.GetData();
            pivotGridControl1.OptionsData.CustomObjectConverter = new CustomObjectConverter();
        }

        // Handles the Save button's Click event to save pivot grid data to a stream
        // (requires data source serialization).
        private void simpleButton1_Click(object sender, EventArgs e) {
            if (stream != null) stream.Dispose();
            stream = new MemoryStream();
            pivotGridControl1.SavePivotGridToStream(stream);
        }

        // Handles the Load button's Click event to load pivot grid data from a stream
        // (requires stream content deserialization).
        private void simpleButton2_Click(object sender, EventArgs e) {
            if (stream == null) return;
            PivotFileDataSource ds = new PivotFileDataSource(stream, new CustomObjectConverter());
            pivotGridControl1.DataSource = ds;
        }
    }

    // Implements a custom serializer.
    public class CustomObjectConverter : ICustomObjectConverter {

        // Returns a value, indicating whether objects of the specified type
        // can be serialized/deserialized.
        public bool CanConvert(Type type) {
            return type == typeof(Employee);
        }

        // Deserializes objects of the specified type.
        public object FromString(Type type, string str) {
            if (type != typeof(Employee))
                return null;
            string[] array = str.Split('#');
            if (array.Length >= 3)
                return new Employee(array[0], array[1], int.Parse(array[2]));
            else if (array.Length == 2)
                return new Employee(array[0], array[1], 0);
            else if (array.Length == 1)
                return new Employee(array[0], string.Empty, 0);
            else
                return new Employee(string.Empty, string.Empty, 0);
        }

        // Serializes objects of the specified type.
        public string ToString(Type type, object obj) {
            if (type != typeof(Employee))
                return string.Empty;
            Employee value = obj as Employee;
            return value.FirstName + '#' + value.LastName + '#' + value.Age;
        }

        // Returns the type by its full name.
        public Type GetType(string typeName) {
            if (typeName != typeof(Employee).FullName)
                return null;
            return typeof(Employee);
        }
    }
}
See Also