Skip to main content

PivotSerializationOptions.AddNewGroups Attached Property

Gets or sets whether the groups that currently exist in the PivotGrid, but do not exist in a layout when it’s restored, should be retained. This is an attached property.

Namespace: DevExpress.Xpf.PivotGrid

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

NuGet Package: DevExpress.Wpf.PivotGrid

Declaration

Returns

Type Description
Boolean

true to retain the groups that currently exist in the PivotGrid but don’t exist in the layout being restored; false to destroy these groups.

Remarks

This option affects how the PivotGrid’s layout is restored. If the AddNewGroups property is set to true, the groups that exist in the current layout but do not exist in the layout being restored, will be retained. Otherwise, these groups will be destroyed.

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:

Manage layout example

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:

Upgrade Pivot Grid Layout

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);
            };
        }
    }
}
<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>
See Also