Back to Devexpress

PivotGridControl.RestorePivotGridFromFile(String) Method

wpf-devexpress-dot-xpf-dot-pivotgrid-dot-pivotgridcontrol-dot-restorepivotgridfromfile-x28-system-dot-string-x29.md

latest7.6 KB
Original Source

PivotGridControl.RestorePivotGridFromFile(String) Method

SECURITY-RELATED CONSIDERATIONS

Deserializing layout settings from untrusted resources may create security issues. Review the following help topic for additional information: Safe Deserialization.

Restores the PivotGridControl’s data and layout from the specified file.

Namespace : DevExpress.Xpf.PivotGrid

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

NuGet Package : DevExpress.Wpf.PivotGrid

Declaration

csharp
public void RestorePivotGridFromFile(
    string path
)
vb
Public Sub RestorePivotGridFromFile(
    path As String
)

Parameters

NameTypeDescription
pathString

A String value that specifies the path to the file which contains the pivot grid’s data and layout.

|

Remarks

To save the pivot grid to a file, use the PivotGridControl.SavePivotGridToFile method.

To learn more, see Save and Restore Layout.

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

PivotGridControl Class

PivotGridControl Members

DevExpress.Xpf.PivotGrid Namespace