I cannot seem to reproduce this issue within a smaller project, but I have a Top docked panel in a user control, and a panel below it containing a grid. If I set the Visible state of the Top docked panel to True in the designer then the InitializeLayout event is triggered before the columns have all been initialized. However, if this panel is set to False, then the InitializeLayout event is NOT triggered until the DisplayLayout property is first accessed within code (in my case, within the constructor, but after the default InitializeComponent call. If I change the designer to have call to PerformLayout on the panel containing the grid, before the line that seems to trigger the InitializeLayout event, then this works but is not a permanent solution.
I wish I could re-create this error, but I am hoping just looking at what triggers the InitializeLayout event would be enough to know...?
It would be good if this error could encourage a "columns changed" event? Is it worth putting in a request?
This is why I'm having great difficult in recreating it then. In the designer, if I enter a line to "PerformLayout" before my Panel that is triggering the InitializeLayout to fire, then all the columns have been generated. I cannot figure out why the data source is initialized on the line of code in the designer when it does.
I've often wanted an InitializeColumnsCollection or ColumnsChanged event off the grid so I'll put in a request - for now, I simply have to leave the Panel hidden until after the InitializeLayout is triggered on the grid.
Very strange behaviour and if I ever figure out how to re-create it, I'll re-post with the sample project.
Thanks.
Hi,
I've never heard of that happening before. My best guess is that the data source doesn't have the columns at the time of binding and they are only added to the data source later on.
But there is no event that fires on the grid when a new column is added to the DataSource - in such a case, the grid is the event listener not the initiator - so it's really the DataSource that should be firing an event. Although, I can see how a grid event would be useful in a case like this.
It is only firing a single time as I already tried to check for columns before allowing my InitializeLayout code to process.
I think I'll have to move the code from Initialise layout, to the constructor - that will do the trick.
I just wanted to check whether this is a potential bug, or whether it's worth requesting an event to be fired when the columns collection changes?
InitializeLayout fires when the grid is bound. The BindingManager might be delaying this until the control's handle is created, so in the case where the control it not visible and nothing has asked for the DisplayLayout, it might be delayed until the control paints the first time.
In any case... if the event is firing before the columns are created, then does that mean that you are not seeing all of the columns on the screen? If you see the columns, then it probably means that InitializeLayout is firing more than once, which means you could just check for the existance of the columns you need and if they are not there, bail out. Then you can do the real initialization of the layout when the event fires again.
Another option you could explore is forcing the event to fire by making the control visible or forcing it's handle to be created. But this could be dangerous and cause other problems, so I woudn't recommend it except as a last resort.