Back to Devexpress

How to: Save and Restore Layout to/from XML

wpf-8046-controls-and-libraries-pivot-grid-examples-miscellaneous-how-to-save-and-restore-layout-to-from-xml.md

latest4.8 KB
Original Source

How to: Save and Restore Layout to/from XML

  • Apr 15, 2018
  • 2 minutes to read

The following example shows how to save the Pivot Grid’s layout to a file in XML format. The “Save” button calls the PivotGridControl.SaveLayoutToXml method. The “Load” button calls the PivotGridControl.RestoreLayoutFromXml method to restore the saved layout.

cs
using System.Data;
using System.Data.OleDb;
using System.Windows;
using DevExpress.Xpf.PivotGrid;
using HowToBindToMDB.NwindDataSetTableAdapters;

namespace HowToBindToMDB {
    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 buttonSave_Click(object sender, RoutedEventArgs e) {
            pivotGridControl1.SaveLayoutToXml("layout.xml");
        }
        private void buttonLoad_Click(object sender, RoutedEventArgs e) {
            pivotGridControl1.RestoreLayoutFromXml("layout.xml");
        }
    }
}
vb
Imports System.Windows
Imports HowToBindToMDB.NwindDataSetTableAdapters

Namespace HowToBindToMDB

    Public Partial Class MainWindow
        Inherits Window

        Private salesPersonDataTable As NwindDataSet.SalesPersonDataTable = New NwindDataSet.SalesPersonDataTable()

        Private salesPersonDataAdapter As SalesPersonTableAdapter = New SalesPersonTableAdapter()

        Public Sub New()
            Me.InitializeComponent()
            Me.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)
            Me.pivotGridControl1.SaveLayoutToXml("layout.xml")
        End Sub

        Private Sub buttonLoad_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
            Me.pivotGridControl1.RestoreLayoutFromXml("layout.xml")
        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 />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <dxpg:PivotGridControl Name="pivotGridControl1" DataProcessingEngine="Optimized">
            <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="fieldYear" Area="ColumnArea" Caption="Year">
                    <dxpg:PivotGridField.DataBinding>
                        <dxpg:DataSourceColumnBinding ColumnName="OrderDate" GroupInterval="DateYear"/>
                    </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>
        <StackPanel Grid.Row="1" Orientation="Horizontal">
            <Button Name="buttonSave" Content="Save" Padding="8, 4, 8, 4" 
                    Margin="4" Click="buttonSave_Click" />
            <Button Name="buttonLoad" Content="Load" Padding="8, 4, 8, 4" 
                    Margin="4" Click="buttonLoad_Click" />
        </StackPanel>
    </Grid>
</Window>