corelibraries-devexpress-dot-xtrapivotgrid-dot-pivotgridoptionsdata-51eb1fda.md
Gets or sets a converter used to serialize/deserialize arbitrary objects in a custom manner.
Namespace : DevExpress.XtraPivotGrid
Assembly : DevExpress.PivotGrid.v25.2.Core.dll
NuGet Packages : DevExpress.PivotGrid.Core, DevExpress.Win.Navigation
[Browsable(false)]
public ICustomObjectConverter CustomObjectConverter { get; set; }
<Browsable(False)>
Public Property CustomObjectConverter As ICustomObjectConverter
| Type | Description |
|---|---|
| ICustomObjectConverter |
An object that implements the ICustomObjectConverter interface representing a converter used to serialize/deserialize arbitrary objects in a custom manner.
|
You can access this nested property as listed below:
| Object Type | Path to CustomObjectConverter |
|---|---|
| PivotGridControl |
.OptionsData .CustomObjectConverter
|
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.
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 Employee class 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);
}
}
}
Imports Microsoft.VisualBasic
Imports System
Imports System.IO
Imports System.Windows.Forms
Imports DevExpress.Data.PivotGrid
Imports DevExpress.Utils.Serializing.Helpers
Namespace XtraPivotGrid_CustomObjectConverter
Partial Public Class Form1
Inherits Form
Private stream As MemoryStream
Public Sub New()
InitializeComponent()
pivotGridControl1.DataSource = DataHelper.GetData()
pivotGridControl1.OptionsData.CustomObjectConverter = _
New CustomObjectConverter()
End Sub
' Handles the Save button's Click event to save pivot grid data to a stream
' (requires data source serialization).
Private Sub simpleButton1_Click(ByVal sender As Object, ByVal e As EventArgs) _
Handles simpleButton1.Click
If stream IsNot Nothing Then
stream.Dispose()
End If
stream = New MemoryStream()
pivotGridControl1.SavePivotGridToStream(stream)
End Sub
' Handles the Load button's Click event to load pivot grid data from a stream
' (requires stream content deserialization).
Private Sub simpleButton2_Click(ByVal sender As Object, ByVal e As EventArgs) _
Handles simpleButton2.Click
If stream Is Nothing Then
Return
End If
Dim ds As New PivotFileDataSource(stream, New CustomObjectConverter())
pivotGridControl1.DataSource = ds
End Sub
End Class
' Implements a custom serializer.
Public Class CustomObjectConverter
Implements ICustomObjectConverter
' Returns a value, indicating whether objects of the specified type
' can be serialized/deserialized.
Public Function CanConvert(ByVal type As Type) As Boolean _
Implements ICustomObjectConverter.CanConvert
Return type Is GetType(Employee)
End Function
' Deserializes objects of the specified type.
Public Function FromString(ByVal type As Type, ByVal str As String) As Object _
Implements ICustomObjectConverter.FromString
If type IsNot GetType(Employee) Then
Return Nothing
End If
Dim array() As String = str.Split("#"c)
If array.Length >= 3 Then
Return New Employee(array(0), array(1), Integer.Parse(array(2)))
ElseIf array.Length = 2 Then
Return New Employee(array(0), array(1), 0)
ElseIf array.Length = 1 Then
Return New Employee(array(0), String.Empty, 0)
Else
Return New Employee(String.Empty, String.Empty, 0)
End If
End Function
' Serializes objects of the specified type.
Public Overloads Function ToString(ByVal type As Type, ByVal obj As Object) As String _
Implements ICustomObjectConverter.ToString
If type IsNot GetType(Employee) Then
Return String.Empty
End If
Dim value As Employee = TryCast(obj, Employee)
Return value.FirstName + "#"c + value.LastName + "#"c + value.Age
End Function
' Returns the type by its full name.
Public Overloads Function [GetType](ByVal typeName As String) As Type _
Implements ICustomObjectConverter.GetType
If typeName IsNot GetType(Employee).FullName Then
Return Nothing
End If
Return GetType(Employee)
End Function
End Class
End Namespace
See Also