恢复 DevExpress GridControl 的列

Restoring a column of a DevExpress GridControl

我有一个 DevExpress GridView,我在其中添加了一列,然后保存布局:

private void button1_Click(object sender, EventArgs e)
{
    // This magically creates a column which I don't want, so delete it
    gridControl1.DataSource = new ObservableCollection<object> { new object() };
    var gridView = ((GridView)gridControl1.MainView);
    gridView.Columns.Clear();

    // Create the column I want
    var myColumn = new GridColumn { Name = "MyColumn", Caption = "MyColumn" };
    gridView.Columns.Add(myColumn);
    // At this point, the column is not displayed yet 
    // And it's not displayed in the column chooser panel
    gridView.Columns[0].Visible = true;
    // The column is visible now and available in the column chooser

    // Awesome layout now, let's save it
    using (var stream = new MemoryStream())
    {
        gridControl1.MainView.SaveLayoutToStream(stream, GetLayoutOptions());
        layoutXml = Encoding.UTF8.GetString(stream.GetBuffer(), 0, (int)stream.Length);
    }
}

这提供了一个不错的 XML,其中包含我的专栏:

<XtraSerializer version="1.0" application="View">
...
  <property name="Columns" iskey="true" value="1">
    <property name="Item1" isnull="true" iskey="true">
      <property name="Visible">true</property>
      <property name="VisibleIndex">0</property>
      <property name="Name">MyColumn</property>
    </property>
  </property>
...
</XtraSerializer>

然后,用户可以做其他事情,例如更改布局

private void button2_Click(object sender, EventArgs e)
{
    var gridView = ((GridView)gridControl1.MainView);
    gridView.Columns.Clear();
}

回到主题,他想看 "old" 视图:

private void button3_Click(object sender, EventArgs e)
{
    using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(layoutXml)))
    {
        gridControl1.MainView.RestoreLayoutFromStream(stream, GetLayoutOptions());
    }
}

但该列未显示。在调试器中查看 ((GridView)gridControl1.MainView).Columns 显示列数为 0。

做了一些研究,我在 DevExpress Forum 中发现我需要在保存和读取布局时设置选项,所以我调整了我的代码以包含建议的方法(已在代码中使用)以上):

private OptionsLayoutGrid GetLayoutOptions() {
    OptionsLayoutGrid layoutGrid = new OptionsLayoutGrid();
    layoutGrid.StoreAllOptions = true;
    layoutGrid.StoreAppearance = true;
    return layoutGrid;
}

仍然没有运气。

如何取回布局的列?

我知道我可以自己解析 XML 并以编程方式添加列,但是......好吧......我希望这是 [=44 的内置功能=].

添加

layoutGrid.Columns.AddNewColumns = true;

another DevExpress Forum post 中的提议也无济于事。

由于要检索的列存在于保存的布局中,但不存在于网格中,因此有必要设置 OptionsLayoutGrid.Columns.RemoveOldColumns 属性 为假。在这种情况下,应该启用 OptionsLayoutGrid.Columns.StoreAllOptions 选项。