Back to Devexpress

PivotGridControl.LoadCollapsedStateFromFile(String) Method

windowsforms-devexpress-dot-xtrapivotgrid-dot-pivotgridcontrol-dot-loadcollapsedstatefromfile-x28-system-dot-string-x29.md

latest9.0 KB
Original Source

PivotGridControl.LoadCollapsedStateFromFile(String) 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.

Restores the collapsed state of field values from the specified file.

Namespace : DevExpress.XtraPivotGrid

Assembly : DevExpress.XtraPivotGrid.v25.2.dll

NuGet Package : DevExpress.Win.PivotGrid

Declaration

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

Parameters

NameTypeDescription
pathString

A String value that specifies the path to the file from which the collapsed state of field values is read. If the specified file does not exist, an exception is raised.

|

Remarks

Use the LoadCollapsedStateFromFile method to load the collapsed state of field values that has been written to a file via the PivotGridControl.SaveCollapsedStateToFile method.

The collapsed states of field values can only be restored in the same layout they were saved in. If the control layout was changed after saving the collapsed states (for instance, some fields have been reordered or hidden), the LoadCollapsedStateFromFile method may have no effect, or may restore the collapsed states incorrectly. To ensure that the collapsed states are loaded correctly, prevent end-users from changing the control layout (use the PivotGridOptionsCustomization.AllowDrag and PivotGridOptionsCustomization.AllowHideFields properties), or save and load the field values’ collapsed states together with the control layout (use the PivotGridControl.SaveLayoutToXml and PivotGridControl.RestoreLayoutFromXml methods).

Note

The LoadCollapsedStateFromFile method has no effect if used when data is not loaded (i.e. the PivotGridControl.DataSource property is not initialized).

Note

The collapsed states for server mode and regular data sources are stored in different formats and not compatible with each other. Some issues can appear on restoring the collapsed state from different data source modes.

Example

This example demonstrates how to restore the Pivot Grid layout and row/column state.

View Example

The Save button uses the PivotGridControl.SaveLayoutToStream and PivotGridControl.SaveCollapsedStateToStream methods to save the layout and field values’ collapsed state to memory streams.

The Load button uses the PivotGridControl.RestoreLayoutFromStream and PivotGridControl.LoadCollapsedStateFromStream methods to restore the saved layout and column/row state.

The Clear button clears the field collection.

cs
using System;
using System.IO;
using System.Windows.Forms;
using DevExpress.Utils;

namespace XtraPivotGrid_RestoreLayoutExample {
    public partial class Form1 : DevExpress.XtraEditors.XtraForm {
        MemoryStream layoutStream = new MemoryStream();
        MemoryStream collapseStateStream = new MemoryStream();
        public Form1() {
            InitializeComponent();
            btnClear.Enabled = false;
        }
        private void Form1_Load(object sender, EventArgs e) {
            excelDataSource1.FileName = "SalesPerson.xlsx";
            excelDataSource1.Fill();
            pivotGridControl1.DataSource = excelDataSource1;
        }
        private void btnSave_Click(object sender, EventArgs e) {
            if (pivotGridControl1.Fields.Count > 0) {
                layoutStream.Dispose();
                layoutStream = new MemoryStream();
                pivotGridControl1.SaveLayoutToStream(layoutStream, OptionsLayoutBase.FullLayout);
                collapseStateStream.Dispose();
                collapseStateStream = new MemoryStream();
                pivotGridControl1.SaveCollapsedStateToStream(collapseStateStream);
                btnClear.Enabled = true;
            }
        }
        private void btnLoad_Click(object sender, EventArgs e) {
            if (layoutStream.Length > 0 && collapseStateStream.Length > 0) {
                layoutStream.Seek(0, SeekOrigin.Begin);
                pivotGridControl1.RestoreLayoutFromStream(layoutStream, OptionsLayoutBase.FullLayout);
                collapseStateStream.Seek(0, SeekOrigin.Begin);
                pivotGridControl1.LoadCollapsedStateFromStream(collapseStateStream);
            }
        }
        private void btnClear_Click(object sender, EventArgs e) {
            if (layoutStream.Length > 0 && collapseStateStream.Length > 0) {
                pivotGridControl1.Fields.Clear();
            }
        }
    }
}
vb
Imports System
Imports System.IO
Imports System.Windows.Forms
Imports DevExpress.Utils

Namespace XtraPivotGrid_RestoreLayoutExample
    Partial Public Class Form1
        Inherits DevExpress.XtraEditors.XtraForm

        Private layoutStream As New MemoryStream()
        Private collapseStateStream As New MemoryStream()
        Public Sub New()
            InitializeComponent()
            btnClear.Enabled = False
        End Sub
        Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
            excelDataSource1.FileName = "SalesPerson.xlsx"
            excelDataSource1.Fill()
            pivotGridControl1.DataSource = excelDataSource1
        End Sub
        Private Sub btnSave_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSave.Click
            If pivotGridControl1.Fields.Count > 0 Then
                layoutStream.Dispose()
                layoutStream = New MemoryStream()
                pivotGridControl1.SaveLayoutToStream(layoutStream, OptionsLayoutBase.FullLayout)
                collapseStateStream.Dispose()
                collapseStateStream = New MemoryStream()
                pivotGridControl1.SaveCollapsedStateToStream(collapseStateStream)
                btnClear.Enabled = True
            End If
        End Sub
        Private Sub btnLoad_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnLoad.Click
            If layoutStream.Length > 0 AndAlso collapseStateStream.Length > 0 Then
                layoutStream.Seek(0, SeekOrigin.Begin)
                pivotGridControl1.RestoreLayoutFromStream(layoutStream, OptionsLayoutBase.FullLayout)
                collapseStateStream.Seek(0, SeekOrigin.Begin)
                pivotGridControl1.LoadCollapsedStateFromStream(collapseStateStream)
            End If
        End Sub
        Private Sub btnClear_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnClear.Click
            If layoutStream.Length > 0 AndAlso collapseStateStream.Length > 0 Then
                pivotGridControl1.Fields.Clear()
            End If
        End Sub
    End Class
End Namespace

See Also

SaveCollapsedStateToFile(String)

LoadCollapsedStateFromStream(Stream)

PivotGridControl Class

PivotGridControl Members

DevExpress.XtraPivotGrid Namespace