Back to Devexpress

How to: Save a Grid's Layout Between Application Runs

windowsforms-3044-controls-and-libraries-data-grid-examples-miscellaneous-how-to-save-a-grids-layout-between-application-runs.md

latest2.3 KB
Original Source

How to: Save a Grid's Layout Between Application Runs

  • Aug 01, 2019
  • 2 minutes to read

Tip

You can utilize the Persistence Behavior or Workspace Manager component to save and restore layouts for all supported DevExpress controls at once.

This example shows how to save and restore the layout of the GridControl.MainView between application runs. The form’s Load event is handled to restore the layout previously saved to an XML file. The form’s Closing event is handled to save the current layout to the specified file.

Note that the GridControl.ForceInitialize method is called before the layout is restored in the Form.Load event. This method needs to be called before a grid’s settings are changed in the Form.Load event. It finishes the control’s initialization and thus ensures that all the changes that will be made do not conflict with the options that have been customized at design time.

csharp
using DevExpress.XtraGrid;
// ...
string fileName = "c:\\XtraGrid_SaveLayoutToXML.xml";

private void Form1_Load(object sender, System.EventArgs e) {
   gridControl1.ForceInitialize();
   // Restore the previously saved layout
   gridControl1.MainView.RestoreLayoutFromXml(fileName);
}

private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e) {
   // Save the layout to an XML file
   gridControl1.MainView.SaveLayoutToXml(fileName);
}
vb
Imports DevExpress.XtraGrid
' ...
Dim fileName As String = "c:\XtraGrid_SaveLayoutToXML.xml"

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _
  Handles MyBase.Load
   GridControl1.ForceInitialize()
   ' Restore the previously saved layout
   GridControl1.MainView.RestoreLayoutFromXml(fileName)
End Sub

Private Sub Form1_Closing(ByVal sender As Object, _
  ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
   ' Save the layout to an XML file
   GridControl1.MainView.SaveLayoutToXml(fileName)
End Sub