Back to Devexpress

How to: Save and Load Field Values' Collapsed States Together With Pivot Grid's Layout

aspnet-9179-components-pivot-grid-examples-miscellaneous-how-to-save-and-load-field-values-collapsed-states-together-with-pivot-grids-layout.md

latest11.1 KB
Original Source

How to: Save and Load Field Values' Collapsed States Together With Pivot Grid's Layout

  • Dec 17, 2020
  • 3 minutes to read

Field values' collapsed states can be restored only in the same layout they have been saved in. This example shows how to save and load a control's layout together with collapsed states to ensure that the states are loaded in the appropriate layout.

csharp
using System;
using System.IO;
using System.Web.UI;
using DevExpress.Utils;
using DevExpress.Web.ASPxPivotGrid;

namespace ASPxPivotGrid_SaveLoadCollapsedState {
    public partial class _Default : Page {
        protected void btnSave_Click(object sender, EventArgs e) {
            Session["Layout"] = ASPxPivotGrid1.SaveLayoutToString( PivotGridWebOptionsLayout.DefaultLayout );
            MemoryStream collapseStateStream = (MemoryStream)(Session["CollapseStateStream"]);
            if (collapseStateStream != null) {
                collapseStateStream.Dispose();
            }
            collapseStateStream = new MemoryStream();
            ASPxPivotGrid1.SaveCollapsedStateToStream(collapseStateStream);
            Session["CollapseStateStream"] = collapseStateStream;
        }
        protected void btnLoad_Click(object sender, EventArgs e) {
            MemoryStream collapseStateStream = (MemoryStream)(Session["CollapseStateStream"]);
            string layout = (string)(Session["Layout"]);
            if (layout == null ||
                collapseStateStream == null) {
                return;
            }
            ASPxPivotGrid1.LoadLayoutFromString(layout, PivotGridWebOptionsLayout.DefaultLayout );
            collapseStateStream.Seek(0, SeekOrigin.Begin);
            ASPxPivotGrid1.LoadCollapsedStateFromStream(collapseStateStream);
        }
        protected void btnClear_Click(object sender, EventArgs e) {
            ASPxPivotGrid1.Fields.Clear();
        }
    }
}
aspx
<%@ Page Language="C#" AutoEventWireup="true" Codebehind="Default.aspx.cs"
    Inherits="ASPxPivotGrid_SaveLoadCollapsedState._Default" %>
<%@ Register Assembly="DevExpress.Web.v13.1, Version=13.1.8.0,
    Culture=neutral, PublicKeyToken=b88d1754d700e49a"
    Namespace="DevExpress.Web.ASPxEditors"
    TagPrefix="dx" %>
<%@ Register Assembly="DevExpress.Web.ASPxPivotGrid.v13.1, Version=13.1.8.0,
    Culture=neutral, PublicKeyToken=b88d1754d700e49a"
    Namespace="DevExpress.Web.ASPxPivotGrid"
    TagPrefix="dx" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <table>
                <tbody>
                    <tr>
                        <td>
                            <dx:ASPxButton ID="btnSave" runat="server" Text="Save"
                            OnClick="btnSave_Click" />
                        </td>
                        <td>
                            <dx:ASPxButton ID="btnLoad" runat="server" Text="Load"
                            OnClick="btnLoad_Click" />
                        </td>
                        <td>
                            <dx:ASPxButton ID="btnClear" runat="server" Text="Clear"
                            OnClick="btnClear_Click" />
                        </td>
                    </tr>
                </tbody>
            </table>
            <dx:ASPxPivotGrid ID="ASPxPivotGrid1" runat="server" DataSourceID="AccessDataSource1"
                Width="677px" OptionsView-ShowHorizontalScrollBar="true">
                <Fields>
                    <dx:PivotGridField ID="fieldProductName" Area="RowArea" AreaIndex="0"
                        Caption="Product Name" FieldName="ProductName">
                    </dx:PivotGridField>
                    <dx:PivotGridField ID="fieldYear" Area="ColumnArea" AreaIndex="0" Caption="Year"
                        FieldName="OrderDate" GroupIndex="0" GroupInterval="DateYear"
                        InnerGroupIndex="0" UnboundFieldName="fieldYear">
                    </dx:PivotGridField>
                    <dx:PivotGridField ID="fieldExtendedPrice" Area="DataArea" AreaIndex="0"
                        Caption="Extended Price" FieldName="ExtendedPrice">
                    </dx:PivotGridField>
                    <dx:PivotGridField ID="fieldQuantity" Area="DataArea" AreaIndex="1"
                        Caption="Quantity" FieldName="Quantity">
                    </dx:PivotGridField>
                    <dx:PivotGridField ID="fieldMonth" Area="ColumnArea" AreaIndex="2" Caption="Month"
                        FieldName="OrderDate" GroupIndex="0" InnerGroupIndex="2"
                        GroupInterval="DateMonth" UnboundFieldName="fieldMonth">
                    </dx:PivotGridField>
                    <dx:PivotGridField ID="fieldQuarter" Area="ColumnArea" AreaIndex="1"
                        Caption="Quarter" FieldName="OrderDate" GroupIndex="0"
                        GroupInterval="DateQuarter" InnerGroupIndex="1"
                        UnboundFieldName="fieldQuarter">
                    </dx:PivotGridField>
                </Fields>
                <Groups>
                    <dx:PivotGridWebGroup />
                </Groups>
            </dx:ASPxPivotGrid>
            <asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="~/nwind.mdb"
                SelectCommand="SELECT [ProductName], [ExtendedPrice], [Quantity],
                [OrderDate] FROM [Invoices]">
            </asp:AccessDataSource>
        </div>
    </form>
</body>
</html>
aspx
<%@ Page Language="vb" AutoEventWireup="true" Codebehind="Default.aspx.vb"
    Inherits="ASPxPivotGrid_SaveLoadCollapsedState._Default" %>
<%@ Register Assembly="DevExpress.Web.v13.1, Version=13.1.8.0,
    Culture=neutral, PublicKeyToken=b88d1754d700e49a"
    Namespace="DevExpress.Web.ASPxEditors"
    TagPrefix="dx" %>
<%@ Register Assembly="DevExpress.Web.ASPxPivotGrid.v13.1, Version=13.1.8.0,
    Culture=neutral, PublicKeyToken=b88d1754d700e49a"
    Namespace="DevExpress.Web.ASPxPivotGrid"
    TagPrefix="dx" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <table>
                <tbody>
                    <tr>
                        <td>
                            <dx:ASPxButton ID="btnSave" runat="server" Text="Save"
                            OnClick="btnSave_Click" />
                        </td>
                        <td>
                            <dx:ASPxButton ID="btnLoad" runat="server" Text="Load"
                            OnClick="btnLoad_Click" />
                        </td>
                        <td>
                            <dx:ASPxButton ID="btnClear" runat="server" Text="Clear"
                            OnClick="btnClear_Click" />
                        </td>
                    </tr>
                </tbody>
            </table>
            <dx:ASPxPivotGrid ID="ASPxPivotGrid1" runat="server" DataSourceID="AccessDataSource1"
                Width="677px" OptionsView-ShowHorizontalScrollBar="true">
                <Fields>
                    <dx:PivotGridField ID="fieldProductName" Area="RowArea" AreaIndex="0"
                        Caption="Product Name" FieldName="ProductName">
                    </dx:PivotGridField>
                    <dx:PivotGridField ID="fieldYear" Area="ColumnArea" AreaIndex="0" Caption="Year"
                        FieldName="OrderDate" GroupIndex="0" GroupInterval="DateYear"
                        InnerGroupIndex="0" UnboundFieldName="fieldYear">
                    </dx:PivotGridField>
                    <dx:PivotGridField ID="fieldExtendedPrice" Area="DataArea" AreaIndex="0"
                        Caption="Extended Price" FieldName="ExtendedPrice">
                    </dx:PivotGridField>
                    <dx:PivotGridField ID="fieldQuantity" Area="DataArea" AreaIndex="1"
                        Caption="Quantity" FieldName="Quantity">
                    </dx:PivotGridField>
                    <dx:PivotGridField ID="fieldMonth" Area="ColumnArea" AreaIndex="2" Caption="Month"
                        FieldName="OrderDate" GroupIndex="0" InnerGroupIndex="2"
                        GroupInterval="DateMonth" UnboundFieldName="fieldMonth">
                    </dx:PivotGridField>
                    <dx:PivotGridField ID="fieldQuarter" Area="ColumnArea" AreaIndex="1"
                        Caption="Quarter" FieldName="OrderDate" GroupIndex="0"
                        GroupInterval="DateQuarter" InnerGroupIndex="1"
                        UnboundFieldName="fieldQuarter">
                    </dx:PivotGridField>
                </Fields>
                <Groups>
                    <dx:PivotGridWebGroup />
                </Groups>
            </dx:ASPxPivotGrid>
            <asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="~/nwind.mdb"
                SelectCommand="SELECT [ProductName], [ExtendedPrice], [Quantity],
                [OrderDate] FROM [Invoices]">
            </asp:AccessDataSource>
        </div>
    </form>
</body>
</html>
vb
Imports Microsoft.VisualBasic
Imports System
Imports System.IO
Imports System.Web.UI
Imports DevExpress.Utils
Imports DevExpress.Web.ASPxPivotGrid

Namespace ASPxPivotGrid_SaveLoadCollapsedState
    Partial Public Class _Default
        Inherits Page
        Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As EventArgs)
            Session("Layout") = ASPxPivotGrid1.SaveLayoutToString(PivotGridWebOptionsLayout.DefaultLayout)
            Dim collapseStateStream As MemoryStream = CType(Session("CollapseStateStream"), MemoryStream)
            If collapseStateStream IsNot Nothing Then
                collapseStateStream.Dispose()
            End If
            collapseStateStream = New MemoryStream()
            ASPxPivotGrid1.SaveCollapsedStateToStream(collapseStateStream)
            Session("CollapseStateStream") = collapseStateStream
        End Sub
        Protected Sub btnLoad_Click(ByVal sender As Object, ByVal e As EventArgs)
            Dim collapseStateStream As MemoryStream = CType(Session("CollapseStateStream"), MemoryStream)
            Dim layout As String = CStr(Session("Layout"))
            If layout Is Nothing OrElse collapseStateStream Is Nothing Then
                Return
            End If
            ASPxPivotGrid1.LoadLayoutFromString(layout, PivotGridWebOptionsLayout.DefaultLayout)
            collapseStateStream.Seek(0, SeekOrigin.Begin)
            ASPxPivotGrid1.LoadCollapsedStateFromStream(collapseStateStream)
        End Sub
        Protected Sub btnClear_Click(ByVal sender As Object, ByVal e As EventArgs)
            ASPxPivotGrid1.Fields.Clear()
        End Sub
    End Class
End Namespace