我有一个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();
}回到话题上,他想看到“老”的观点:
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论坛中发现在保存和读取布局时需要设置选项,所以我调整了代码以包括建议的方法(已经在上面的代码中使用了):
private OptionsLayoutGrid GetLayoutOptions() {
OptionsLayoutGrid layoutGrid = new OptionsLayoutGrid();
layoutGrid.StoreAllOptions = true;
layoutGrid.StoreAppearance = true;
return layoutGrid;
}还是没有运气。
如何获得布局的回列?
我知道我可以自己解析XML并以编程方式添加列,但是.嗯..。我认为这是GridControl/GridView的内置功能。
添加
layoutGrid.Columns.AddNewColumns = true;正如另一个DevExpress论坛帖子中所建议的那样,也没有帮助。
发布于 2015-07-17 09:08:42
由于希望检索的列在保存的布局中存在,但在网格中不存在,因此有必要将OptionsLayoutGrid.Columns.RemoveOldColumns属性设置为false。在这种情况下,应该启用OptionsLayoutGrid.Columns.StoreAllOptions选项。
https://stackoverflow.com/questions/31463310
复制相似问题