I've got an app that automatically generates columns from a metadata file, then loads the user's saved layout using the LoadFromXml method. I'm having a problem when a new column is added to the metadata. The newly added column appears in the grid after the layout is loaded, but it seems to lose settings that were previously applied; it just breaks in a few strange ways.
I've found a way to mostly get around it, but it's not quite perfect. I follow the advice in this post, instantiating a new DisplayLayout object, load it from xml, and determine which Column objects are new based on their Key values. Then I use these statements to add the new column to the loaded DisplayLayout:
cols[key].Header.VisiblePosition = band.Columns.Count;band.Columns.Add(cols[key]);
The problem is that while the new column is visible at the end of the grid and it doesn't seem to have its style or bindings broken or anything, the saved order of the columns doesn't get used. Instead it reverts to the original sorting of the columns. I've tested this multiple times by just removing/adding the column declaration from my metadata file. If the unexpected column isn't there, the layout is loaded successfully. My guess is that it's because the Column being added has the same Index value as an existing Column, but this is a read only value. Is there a fix for this, or another solution to the problem of loading a layout when there are new columns?
Hi,
The overload of Columns.Add that you are using here is not intended for use in your code. It's hidden from Intellisense on purpose, because it's only supposed to be used internally by the grid for deserialization purposes.
If you load your layout directly into the grid and the new column is displaying, but some of it's property settings are wrong, then it's sounds like something is wrong there.
But ultimately, there's really no way to maintain an existing column and add it into the grid all in one shot like you are trying to do here.You should add the column back into the grid and set all of it's properties up the way you want them in code rather than relying on design-time settings.
Okay, thanks for the quick response. I worked around this issue by allowing it to load the layout directly into the current DisplayLayout, then looking for any columns that weren't in the xml file and making sure their settings were correct.
For what its worth, the Add method of Columns was/is listed in the Intellisense options on my machine.