How to: Implement a Custom Serializer

  • 8 min to read

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 data is sorted or filtered. You can also use the Save and Load buttons to save the pivot grid layout to a string and restored it, respectively, which also implies field values serialization with the custom serializer.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs"
Inherits="ASPxPivotGrid_CustomObjectConverter._Default" %>

<%@ Register Assembly="DevExpress.Web.ASPxEditors.v11.1, Version=11.1.5.0, 
Culture=neutral, PublicKeyToken=b88d1754d700e49a"
Namespace="DevExpress.Web.ASPxEditors" TagPrefix="dx" %>

<%@ Register Assembly="DevExpress.Web.ASPxPivotGrid.v11.1, Version=11.1.5.0,
Culture=neutral, PublicKeyToken=b88d1754d700e49a"
Namespace="DevExpress.Web.ASPxPivotGrid" TagPrefix="dx" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <table>
                <tr>
                    <td>
                        <table>
                            <tr>
                                <td>
                                    <dx:ASPxButton ID="ASPxButton1" runat="server"
                                        Text="Save" OnClick="ASPxButton1_Click">
                                    </dx:ASPxButton>
                                </td>
                                <td>
                                    <dx:ASPxButton ID="ASPxButton2" runat="server"
                                        Text="Load" OnClick="ASPxButton2_Click">
                                    </dx:ASPxButton>
                                </td>
                            </tr>
                        </table>
                    </td>
                </tr>
                <tr>
                    <td>
                        <dx:ASPxPivotGrid ID="ASPxPivotGrid1" runat="server">
                            <Fields>
                                <dx:PivotGridField ID="fieldProductName" Area="RowArea"
                                    AreaIndex="0" Caption="Product Name"
                                    FieldName="ProductName">
                                </dx:PivotGridField>
                                <dx:PivotGridField ID="fieldSalesPerson" Area="ColumnArea"
                                    AreaIndex="0" Caption="Sales Person"
                                    FieldName="SalesPerson">
                                </dx:PivotGridField>
                                <dx:PivotGridField ID="fieldQuantity" Area="DataArea"
                                    AreaIndex="0" Caption="Quantity"
                                    FieldName="Quantity">
                                </dx:PivotGridField>
                            </Fields>
                        </dx:ASPxPivotGrid>
                    </td>
                </tr>
            </table>
        </div>
    </form>
</body>
</html>