本示例展示了如何在应用程序运行之间保存和恢复 GridControl.MainView 的布局。 接管了窗体的 Load 事件来恢复之前已保存到 XML 文件中的布局。 接管了窗体的 Closing 事件来把当前布局保存到指定的文件中。

注意,在 Form.Load 事件中,在布局被恢复之前,调用了 GridControl.ForceInitialize 方法。 在 Form.Load 事件中,需要在网格的设置被修改之前调用此方法。 它完成控件的初始化,因而确保所有将要作出的修改不会与在设计时刻已定制的选项产生冲突。

C#CopyCode image复制代码
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);
}
Visual BasicCopyCode image复制代码
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