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

  • 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.

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);
}