Skip to main content
A newer version of this page is available. .

Saving and Restoring Layout

  • 6 minutes to read

Saving and Restoring Layout Basics

The GridControl allows you to save the information on its layout to a data store (an XML file or stream) and restore it when required. This information may include the visibility, position and size of visual elements, filter, sorting, grouping and summary information, etc. Multiple options allow you to specify which settings need to be saved and restored when the layout is saved/restored.

To save the grid’s layout, use the following methods: DataControlBase.SaveLayoutToStream or DataControlBase.SaveLayoutToXml. To restore the layout, use the DataControlBase.RestoreLayoutFromStream or DataControlBase.RestoreLayoutFromXml method.

Note

To correctly save and restore the grid layout, grid columns and bands should be uniquely identified.

Use the x:Name attribute to uniquely identify grid bands and grid columns.

Set the DataControlBase.UseFieldNameForSerialization property to true (it is set to true by default) to uniquely identify the grid columns and use their bound field name as a unique value.

Layout Options

Option

Description

DXSerializer.StoreLayoutMode

Specifies which settings should be saved.

Property Values:

None - nothing is saved or restored.

All - all settings are saved/restored. These include the visual, data-aware, behavior, customization options, etc.

UI (Default Value) - only those settings that are marked with the GridUIProperty attribute are saved/restored. These include visibility state, position and size of columns, sorting and grouping settings, summary information, etc.

To manually control which settings should be serialized, handle the DXSerializer.AllowPropertyEvent:

foreach (GridColumn column in grid.Columns)
    column.AddHandler(DXSerializer.AllowPropertyEvent,
        new AllowPropertyEventHandler(column_AllowProperty));
grid.SaveLayoutToXml("d:\\layout.xml");

// ...

void column_AllowProperty(object sender, AllowPropertyEventArgs e) {
    e.Allow = e.DependencyProperty == GridColumn.ActualWidthProperty ||
                     e.DependencyProperty == GridColumn.FieldNameProperty ||
                     e.DependencyProperty == GridColumn.VisibleProperty;
}

Note

The GridControl does not support serialization of the *Style and *Template properties because of the following points:

  • There is no general way to serialize such complex objects like Style or Template.
  • Generally, serialization is used to save/restore properties that can be changed by an end-user, but there is no capability to change Style or Template at runtime (unless you provide this functionality manually).

Layout Loading Specifics

There are two options that specify what to do with the columns that exist in the layout being loaded, but not in the current grid. By default, the columns that exist in the current grid but not in the layout being loaded are retained. Also, the columns that exist in the layout being loaded but do not exist in the current grid’s layout are destroyed. To change this behavior, use the DataControlSerializationOptions.AddNewColumns and DataControlSerializationOptions.RemoveOldColumns properties.

Note

Detail grids aren’t automatically serialized when saving the master grid’s layout. To learn more, see Master-Detail Mode Limitations.

Example

This example shows how to save the grid layout to a memory stream. To do this, click the ‘Save Layout’ button. Once saved, the grid layout can then be restored by clicking the ‘Restore Layout’ button.

using System.Collections.Generic;
using System.Windows;
using System.Windows.Documents;

namespace DXGrid_GridLayout {
    public partial class Window1 : Window {
        public Window1() {
            InitializeComponent();
        }
    }
}