I currently have some logic in the InitializeLayout event that has to execute each time new data is bound to my grid. My grid's datasource is set to a BindingSource object. When I first set the data source of my BindingSource, everything works fine. However, I am then assigning a new object to the my BindingSource's datasource. At this point neither the InitializeLayout or InitializeRow events fire. In order to get the InitializeLayout event to fire I am calling the DataBind() method on my grid. The problem is that after I do this the InitializeRow event is firing before the InitializeLayout event. I have some processing in InitialzeRow that is dependent upon InitializeLayout firing first. Is there any way to get InitializeLayout to fire first?
Thanks!
Steve
Hi Steve,
My guess is that the InitializeRow calls that you get before InitializeLayout fires will be repeated after InitializeLaytout fires, anyway.
It's always a good idea not to make assumptions about when InitializeRow is going to fire and verify the state you are in. So, for example, if you are adding unbound columns to the grid inside InitializeLayout and referencing those columns inside InitializeRow, you should check the e.Row.Cells.Exists method first to make sure the column is actually there before you try to access it.
Ok, thanks Mike. I will keep that in mind. Also, you are correct about the InitializeRow calls firing a second time after InitializeLayout has completed. I am trying to populate some unbound columns with values in the InitializeRow event. One of the columns is an editable checkbox column, so the user can change the value once the grid renders.
The problem I had was that the checkbox value never changed when the user clicked the checkbox because InitializeRow kept firing when the checkbox was changed, causing the original checkbox value to be assigned in the InitializeRow event again. Let me say that I am not editing the checkbox like normal. The requirement was to not allow the cell to go into edit mode. I am trapping the mouse click and then changing the cell value programmatically if a user clicks the checkbox.
I thought I could fix the problem by checking the ReInitialize property in the InitializeRow event and only set the cell value if it was false. However, some of the processing in my InitializeLayout event is needed in order to determine the cell values that I need to set. So, as I mentioned before, I was hoping InitializeLayout would fire first so that the ReInitialize property would be false.
I am thinking that I may just have to create a custom "DataBind()" method and call my InitializeLayout logic explicitly as the first step (btw, I am creating a user control so that is why a custom method is an option).