Back to Devexpress

PivotGridControl.SavePivotGridToFile(String, Boolean) Method

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

latest8.1 KB
Original Source

PivotGridControl.SavePivotGridToFile(String, Boolean) Method

SECURITY-RELATED CONSIDERATIONS

Using file paths sourced from untrusted input may expose unauthorized files or allow unintended file access. Always validate and normalize all external paths to prevent path manipulation.

Saves the PivotGridControl’s data and layout to the specified file, and allows data to be compressed.

Namespace : DevExpress.Xpf.PivotGrid

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

NuGet Package : DevExpress.Wpf.PivotGrid

Declaration

csharp
public void SavePivotGridToFile(
    string path,
    bool compress
)
vb
Public Sub SavePivotGridToFile(
    path As String,
    compress As Boolean
)

Parameters

NameTypeDescription
pathString

A String value that specifies the path to the file in which the control’s data and layout are saved.

| | compress | Boolean |

true to compress the output file; otherwise, false.

|

Remarks

Use the PivotGridControl.RestorePivotGridFromFile method to restore the layout.

To learn more, see Save and Restore Layout.

Note

Saving PivotGrid’s data to a file or stream is not supported for Server Mode and OLAP data sources.

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