Anyone know how I can persist all of my existing application's grid states easily? I'm trying to use SaveAsXml then load the state at the bottom of my grid's Initialize_layout event. However, finding a good event to save the state is proving to be tricky. For example, if I try to save the state from the AfterColPosChanged event it works fine. However, when the datasource gets changed (onLoad or anywhere after) it fires Initialize_layout. Our Initialize_layout events have many grid state default settings already such as setting the VisiblePosition of a column which also causes AfterColPosChanged to fire and overwrites the user's custom state file. I guess my question is, is there a way to tell if the AfterColPosChanged event was fired by the Initialize_layout event or by the user actually changing the position of a column manually? Any suggestions or other places to save the grid state would be greatly appreciated!
Why not just save the layout when the form closes? There's really no reason to continually save and re-save the layout every time the user makes a change.
The grid's datasource gets assigned multiple times in many different areas. This causes the Initialize layout event to call which has it's own layout modification statements that overwrites the loaded xml one. I think a good way I can manage this is to put a flag at the top and bottom of each initialize layout event to prevent the other events (resize, col position change, etc) from saving the file. Then at the bottom of each Initialize layout event it will load the xml again. This way there are the least amount of changes to a lot of existing code and we won't have to hunt down every datasource assignment to put a save before it. However, we will have to hunt down all the initialize layout events, but that's not so bad. Additionally, I might make it save the layout to another element in the grid's layout collection then save that at form close so i'm not always writing to disk all the time. Ideally, I would like to create another control that inherits the grid, but lets me detect when the datasource is being assigned and also maybe place a button on the grid to let the user reset the layout, but that will probably not happen anytime soon. I hope this makes sense and am open to any tips.
InitializeLayout fires when you change the DataSource or DataMember property of the grid. Also, if you call the SetDataBinding method (which is essentially the same thing).
So you could find all the places in your code where you access one of those properties, or that method and then save the layout immediately before that (assuming the grid's DataSource was not null).