Back to Devexpress

PivotSerializationOptions.RemoveOldFields Attached Property

wpf-devexpress-dot-xpf-dot-pivotgrid-dot-pivotserializationoptions-a358e148.md

latest15.2 KB
Original Source

PivotSerializationOptions.RemoveOldFields Attached Property

Gets or sets whether the fields that exist in a layout when it is restored, but don’t currently exist in the PivotGrid, should be discarded. This is an attached property.

Namespace : DevExpress.Xpf.PivotGrid

Assembly : DevExpress.Xpf.PivotGrid.v25.2.dll

NuGet Package : DevExpress.Wpf.PivotGrid

Declaration

See GetRemoveOldFields(DependencyObject) and SetRemoveOldFields(DependencyObject, Boolean).

Returns

TypeDescription
Boolean

true to discard the fields that exist in the layout being restored but don’t currently exist in the PivotGrid; otherwise, false.

|

Remarks

The RemoveOldFields option affects how the PivotGrid’s layout is restored. If it is set to true , the fields that exist in the layout being loaded, but do not exist in the grid, will not be re-created. Otherwise, these fields will be added to the PivotGrid.

To learn more, see Save and Restore Layout.

Example: Update Pivot Grid Layout on Restore

The example below shows how to manage the layout when you restore the Pivot Grid.

The example contains two Pivot Grid controls with the following fields:

The SaveLayout button uses the PivotGridControl.SaveLayoutToStream method to save the pivotGridControlOld layout to memory streams.

The RestoreLayout button uses the PivotGridControl.RestoreLayoutFromStream method to restore the saved layout to pivotGridControlNew.

The following options allows you to combine fields from different Pivot Grid controls on restore:

The following image shows the resulting layout when you save the pivotGridControlOld layout and restore it to pivotGridControlNew:

cs
using DevExpress.Xpf.PivotGrid;
using System.IO;
using System.Windows;

namespace HowToSaveAndRestoreLayoutFromStream {
    public partial class MainWindow : Window {
        // Create a MemoryStream instance.
        System.IO.Stream LayoutStream = new System.IO.MemoryStream();
        public MainWindow() {
            InitializeComponent();
            PivotGridGroup group = pivotGridControlNew.Groups.Add(fieldYear, fieldMonth);         
        }
        private void buttonSave_Click(object sender, RoutedEventArgs e) {
            // Save the layout to a stream.
            pivotGridControlOld.SaveLayoutToStream(LayoutStream);
        }
        private void buttonLoad_Click(object sender, RoutedEventArgs e) {
            LayoutStream.Position = 0;
            LayoutStream.Seek(0, SeekOrigin.Begin);
            // Load the layout from the stream.
            pivotGridControlNew.RestoreLayoutFromStream(LayoutStream);
        }
        private void pivotGridControlNew_LayoutUpgrade(object sender, PivotLayoutUpgradeEventArgs e) {
            if (e.PreviousVersion == "1.0") {
                var newField = new PivotGridField() {
                    DataBinding = new DataSourceColumnBinding("Quantity"),
                    Caption = "Quantity",
                    Name = "fieldQuantity",
                    Area = FieldArea.DataArea
                };
                pivotGridControlNew.Fields.Add(newField);
            };
        }
    }
}
vb
Imports DevExpress.Xpf.PivotGrid
Imports System.IO
Imports System.Windows

Namespace HowToSaveAndRestoreLayoutFromStream

    Public Partial Class MainWindow
        Inherits Window

        ' Create a MemoryStream instance.
        Private LayoutStream As Stream = New MemoryStream()

        Public Sub New()
            Me.InitializeComponent()
            Dim group As PivotGridGroup = Me.pivotGridControlNew.Groups.Add(Me.fieldYear, Me.fieldMonth)
        End Sub

        Private Sub buttonSave_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
            ' Save the layout to a stream.
            Me.pivotGridControlOld.SaveLayoutToStream(LayoutStream)
        End Sub

        Private Sub buttonLoad_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
            LayoutStream.Position = 0
            LayoutStream.Seek(0, SeekOrigin.Begin)
            ' Load the layout from the stream.
            Me.pivotGridControlNew.RestoreLayoutFromStream(LayoutStream)
        End Sub

        Private Sub pivotGridControlNew_LayoutUpgrade(ByVal sender As Object, ByVal e As PivotLayoutUpgradeEventArgs)
            If Equals(e.PreviousVersion, "1.0") Then
                Dim newField = New PivotGridField() With {.DataBinding = New DataSourceColumnBinding("Quantity"), .Caption = "Quantity", .Name = "fieldQuantity", .Area = FieldArea.DataArea}
                Me.pivotGridControlNew.Fields.Add(newField)
            End If
        End Sub
    End Class
End Namespace
xaml
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition />
        <RowDefinition />
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>
    <dxpg:PivotGridControl Name="pivotGridControlOld" 
                           DataSource="{Binding Path=Data, Source={StaticResource TypedSimpleSource}}"
                           DataProcessingEngine="Optimized"
                           dx:DXSerializer.LayoutVersion="1.0">
        <dxpg:PivotGridControl.Fields>
            <dxpg:PivotGridField Name="fieldCategory" Caption="Category" Area="RowArea">
                <dxpg:PivotGridField.DataBinding>
                    <dxpg:DataSourceColumnBinding ColumnName="CategoryName"/>
                </dxpg:PivotGridField.DataBinding>
            </dxpg:PivotGridField>
            <dxpg:PivotGridField Name="fieldExtendedPrice" Area="DataArea" CellFormat="c0">
                <dxpg:PivotGridField.DataBinding>
                    <dxpg:DataSourceColumnBinding ColumnName="Extended Price"/>
                </dxpg:PivotGridField.DataBinding>
            </dxpg:PivotGridField>
        </dxpg:PivotGridControl.Fields>
    </dxpg:PivotGridControl>
    <dxpg:PivotGridControl Grid.Row="1" Name="pivotGridControlNew" 
                           DataSource="{Binding Path=Data, Source={StaticResource TypedSimpleSource}}" 
                           DataProcessingEngine="Optimized"
                           dx:DXSerializer.LayoutVersion="2.0"
                           dxpg:PivotSerializationOptions.AddNewGroups="True"
                           dxpg:PivotSerializationOptions.AddNewFields="True"
                           dxpg:PivotSerializationOptions.RemoveOldFields="False"
                           LayoutUpgrade="pivotGridControlNew_LayoutUpgrade">
        <dxpg:PivotGridControl.Fields>
        <dxpg:PivotGridField Name="fieldYear" Area="ColumnArea" Caption="Year"
                             Group="{Binding ElementName=group}">
            <dxpg:PivotGridField.DataBinding>
                <dxpg:DataSourceColumnBinding ColumnName="OrderDate" GroupInterval="DateYear"/>
            </dxpg:PivotGridField.DataBinding>
        </dxpg:PivotGridField>
        <dxpg:PivotGridField Name="fieldMonth" Area="ColumnArea" Caption="Month"
                             Group="{Binding ElementName=group}">
        <dxpg:PivotGridField.DataBinding>
            <dxpg:DataSourceColumnBinding ColumnName="OrderDate" GroupInterval="DateMonth"/>
        </dxpg:PivotGridField.DataBinding>
        </dxpg:PivotGridField>
        </dxpg:PivotGridControl.Fields>
    </dxpg:PivotGridControl>
    <StackPanel Grid.Row="2" Orientation="Horizontal" >
        <Button Name="buttonSave" Content="Save" Padding="8, 4, 8, 4" 
                Margin="4" Click="buttonSave_Click" ></Button>
        <Button Name="buttonLoad" Content="Load" Padding="8, 4, 8, 4" 
                Margin="4" Click="buttonLoad_Click"></Button>
    </StackPanel>
</Grid>

Example

This example shows how to save (or restore) the PivotGridControl‘s data and layout to a file. To correctly restore fields if the pivot grid’s PivotGridControl.Fields collection is empty, the PivotSerializationOptions.RemoveOldFields property should be set to false.

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

Namespace HowToBindToMDB
    ''' <summary>
    ''' Interaction logic for MainWindow.xaml
    ''' </summary>
    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 button1_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
            pivotGridControl1.SavePivotGridToFile("pivot.dat", True)
            pivotGridControl1.DataSource = Nothing
            pivotGridControl1.Fields.Clear()
        End Sub

        Private Sub button2_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
            If (Not File.Exists("pivot.dat")) Then
                DXMessageBox.Show("You should save the PivotGrid into a file first")
                Return
            End If
            pivotGridControl1.RestorePivotGridFromFile("pivot.dat")
        End Sub
    End Class
End Namespace
xaml
<Window x:Class="HowToBindToMDB.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:dxpg="http://schemas.devexpress.com/winfx/2008/xaml/pivotgrid"
        Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <dxpg:PivotGridControl dxpg:PivotSerializationOptions.RemoveOldFields="False"
                               HorizontalAlignment="Left" Name="pivotGridControl1" 
                               VerticalAlignment="Top" Margin="0,0,0,8">
            <dxpg:PivotGridControl.Fields>
                <dxpg:PivotGridField Name="fieldCountry" FieldName="Country" Area="RowArea" />
                <dxpg:PivotGridField Name="fieldCustomer" FieldName="Sales Person" Area="RowArea" 
                                     Caption="Customer" />
                <dxpg:PivotGridField Name="fieldYear" FieldName="OrderDate" Area="ColumnArea" 
                                     Caption="Year" GroupInterval="DateYear" />
                <dxpg:PivotGridField Name="fieldCategoryName" FieldName="CategoryName" 
                                     Area="ColumnArea" Caption="Product Category" />
                <dxpg:PivotGridField Name="fieldProductName" FieldName="ProductName" 
                                     Area="FilterArea" Caption="Product Name" />
                <dxpg:PivotGridField Name="fieldExtendedPrice" FieldName="Extended Price" 
                                     Area="DataArea" CellFormat="c0" />
            </dxpg:PivotGridControl.Fields>
        </dxpg:PivotGridControl>
        <StackPanel Orientation="Horizontal" Grid.Row="1">
            <Button Content="Save and unbind" Height="23" Name="button1" 
                    Grid.Row="1" Click="button1_Click" 
                    Margin="0,0,8,0" Padding="8,0,8,0" />
            <Button Content="Restore" Height="23" Name="button2" 
                    Grid.Row="1" Grid.Column="1" 
                    Click="button2_Click" 
                    Padding="8,0,8,0" />
        </StackPanel>        
    </Grid>
</Window>
csharp
using System.Data;
using System.Data.OleDb;
using System.Windows;
using DevExpress.Xpf.PivotGrid;
using HowToBindToMDB.NwindDataSetTableAdapters;
using System.IO;
using DevExpress.Xpf.Core;

namespace HowToBindToMDB {
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window {
        NwindDataSet.SalesPersonDataTable salesPersonDataTable = new NwindDataSet.SalesPersonDataTable();
        SalesPersonTableAdapter salesPersonDataAdapter = new SalesPersonTableAdapter();

        public MainWindow() {
            InitializeComponent();
            pivotGridControl1.DataSource = salesPersonDataTable;
        }

        private void Window_Loaded(object sender, RoutedEventArgs e) {
            salesPersonDataAdapter.Fill(salesPersonDataTable);
        }

        private void button1_Click(object sender, RoutedEventArgs e) {
            pivotGridControl1.SavePivotGridToFile("pivot.dat", true);
            pivotGridControl1.DataSource = null;
            pivotGridControl1.Fields.Clear();
        }

        private void button2_Click(object sender, RoutedEventArgs e) {
            if(!File.Exists("pivot.dat")) {
                DXMessageBox.Show("You should save the PivotGrid into a file first");
                return;
            }
            pivotGridControl1.RestorePivotGridFromFile("pivot.dat");
        }
    }
}

See Also

AddNewFields

PivotSerializationOptions Class

PivotSerializationOptions Members

DevExpress.Xpf.PivotGrid Namespace