Using DevExpress tool in very helpful. In this case, I’ve used it for my WPF application. I’m using DevExpress 14.2. After some issues, I am able to save&restore my DataGrid layout with xml files.

First of all, in your GridControl tag, add these attributes:

  • dx:DXSerializer.StoreLayoutMode=”All”
  • dx:DXSerializer.SerializationID=”dgrErog”
  • dxg:GridSerializationOptions.AddNewColumns=”False”
  • dxg:GridSerializationOptions.RemoveOldColumns=”False”

After this, check in your GridColum tags have the Name attribute. If not, add it because when a layout is saved or restored, GridControl uses column names to identify columns. To properly save/restore the layout, it’s necessary to specify column names.
By the way, starting with 15.1, GridControl can use FieldNames for column identification during the serialization process.

So, now your XAML looks like this:

<DockPanel x:Name="pnl_erogazioni" LastChildFill="False" Visibility="{Binding canReadPanels, Converter={StaticResource BoolToVis}}">
    <dxg:GridControl x:Name="dgrErog" ItemsSource="{Binding ErogazioniHomePage}" 
                                dx:DXSerializer.StoreLayoutMode="All" 
                                dx:DXSerializer.SerializationID="dgrErog" 
                                dxg:GridSerializationOptions.AddNewColumns="False" 
                                dxg:GridSerializationOptions.RemoveOldColumns="False">
        <dxg:GridControl.Columns>
            <dxg:GridColumn Binding="{Binding ID}" FieldName="{DynamicResource ResourceKey=Codice}" AllowEditing="False"/>
            <dxg:GridColumn Binding="{Binding TRAN_ID}" FieldName="N° tran." AllowEditing="False"/>
    </dxg:GridControl.Columns>
</dxg:GridControl>

You can see I’m using the binding, because I work with the MVVM patter design, but this method works also without the binding.

After this, we are still missing the functions for save and restore the layout. Below, you can find some samples.

This for save the layout:

private void saveLayoutErogs(object sender, RoutedEventArgs e)
{
    string FileLayoutGridErogs = attivazione.mdlImpostazioni.p.pathProgramData + &amp;quot;\\\\layoutGridErogs.xml&amp;quot;;
    erogs.SaveLayoutToXml(FileLayoutGridErogs);
}

and this for restore it:

        public restoreLayoutErogs(object sender, RoutedEventArgs e)
        {
            string FileLayoutGridErogs = attivazione.mdlImpostazioni.p.pathProgramData + &amp;quot;\\\\layoutGridErogs.xml&amp;quot;;
            try
            {
                erogs.RestoreLayoutFromXml(FileLayoutGridErogs);
            }
            catch (Exception e)
            {}
        }
Advertisements