Reputation: 59575
I have a DevExpress GridView where I add a column, then save the layout:
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);
}
}
This gives a nice XML which contains my column:
<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>
Then, the user can do something else, e.g. change the layout
private void button2_Click(object sender, EventArgs e)
{
var gridView = ((GridView)gridControl1.MainView);
gridView.Columns.Clear();
}
Back on topic, he wants to see the "old" view:
private void button3_Click(object sender, EventArgs e)
{
using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(layoutXml)))
{
gridControl1.MainView.RestoreLayoutFromStream(stream, GetLayoutOptions());
}
}
But the column is not displayed. Looking at ((GridView)gridControl1.MainView).Columns
in the debugger shows a column count of 0.
Doing a bit of research, I found out in the DevExpress Forum that I need to set options when saving and reading the layout, so I adapted my code to include the suggested method (already used in the code above):
private OptionsLayoutGrid GetLayoutOptions() {
OptionsLayoutGrid layoutGrid = new OptionsLayoutGrid();
layoutGrid.StoreAllOptions = true;
layoutGrid.StoreAppearance = true;
return layoutGrid;
}
Still no luck.
How do I get back columns of a layout?
I'm aware that I could parse the XML myself and add the column programmatically, but ... well ... I'd expect that as a built-in functionality of the GridControl/GridView.
Adding
layoutGrid.Columns.AddNewColumns = true;
as proposed in another DevExpress Forum post also doesn't help.
Upvotes: 0
Views: 1831
Reputation: 26
Since the columns you wish to retrieve exist in the saved layout, but do not exist in the grid, it is necessary to set the OptionsLayoutGrid.Columns.RemoveOldColumns property to false. In this case, the OptionsLayoutGrid.Columns.StoreAllOptions option should be enabled.
Upvotes: 1