Skip to main content
A newer version of this page is available. .

Save and Restore Layout

  • 3 minutes to read

A pivot grid’s layout contains settings which determine how its visual elements behave - their size, position within the control, etc. A layout can be saved and then applied to any other Pivot Grid control. This allows you to customize a pivot grid just once, and then apply the saved settings to other pivot grids in other WPF applications.

Base Concepts

DXPivotGrid Specifics

The DXPivotGrid provides multiple options, allowing you to specify which settings should be saved/restored. These options are described below:

Option Description
PivotSerializationOptions.AddNewFields Specifies whether the fields that currently exist in the pivot grid, but do not exist in a layout when it’s restored, should be retained.
PivotSerializationOptions.AddNewGroups Specifies whether the field groups that currently exist in the pivot grid, but do not exist in a layout when it’s restored, should be retained.
PivotSerializationOptions.RemoveOldFields Specifies whether fields that exist in a layout when it is restored, but don’t currently exist in the pivot grid, should be discarded.
PivotSerializationOptions.StoreLayoutMode Gets or sets which settings should be saved when saving the PivotGrid’s layout. This is an attached property.
<dxpg:PivotGridControl Name="pivotGridControl1" dxpg:PivotSerializationOptions.RemoveOldFields="False"/>

To save the layout, use the PivotGridControl.SaveLayoutToXml or PivotGridControl.SaveLayoutToStream method. To restore the previously saved layout, use the PivotGridControl.RestoreLayoutFromXml or PivotGridControl.RestoreLayoutFromStream method.

Important

You should specify unique names for all pivot grid fields using the PivotGridField.Name property to correctly save and restore the layout.

Example: How to Save and Load the DXPivotGrid’s Layout

The following example shows how to save the pivot grid’s layout to a file in XML format. To investigate how this works, click the “Save” button, which calls the PivotGridControl.SaveLayoutToXml method. Once saved, the pivot grid’s layout can then be restored by clicking the “Load” button, which calls the PivotGridControl.RestoreLayoutFromXml method.

Imports Microsoft.VisualBasic
Imports System.Data
Imports System.Data.OleDb
Imports System.Windows
Imports DevExpress.Xpf.PivotGrid
Imports HowToBindToMDB.NwindDataSetTableAdapters

Namespace HowToBindToMDB
    Partial Public Class MainWindow
        Inherits Window
        Private salesPersonDataTable As New NwindDataSet.SalesPersonDataTable()
        Private salesPersonDataAdapter As New SalesPersonTableAdapter()

        Public Sub New()
            InitializeComponent()
            pivotGridControl1.DataSource = salesPersonDataTable
        End Sub

        Private Sub Window_Loaded(ByVal sender As Object, ByVal e As RoutedEventArgs)
            salesPersonDataAdapter.Fill(salesPersonDataTable)
        End Sub

        Private Sub buttonSave_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
            pivotGridControl1.SaveLayoutToXml("layout.xml")
        End Sub

        Private Sub buttonLoad_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
            pivotGridControl1.RestoreLayoutFromXml("layout.xml")
        End Sub
    End Class
End Namespace