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).
It sounds to me like an easier solution would be to just put a check in InitializeRow to see if the unbound column exists, and if not, bail out.
But perhaps that will not work for your particular situation for some reason.
Yeah, I was hoping to do something like that, but it wasn't working for me. The issue I have is that the InitializeRow event is called each time the cell checkbox is changed. This happens when I catch the mouse click and change the cell value to:
cell.Value = !((bool)cell.Value);
When InitializeRow fires it ends up setting my checkbox back to what it originally was because I set the checkbox value to what the underlying datasoure value is. Now, I could get around this if I only set the checkbox value to the underlying datasource value the first time a row is created... which is what I want to do. My initialize layout method is where I figure out all of the editable checkbox columns that I have so that I know which ones to set in the InitializeRow event.
However, since InitialzeRow is firing before InitializeLayout I am having trouble doing this. I don't know which columns are the editable checkboxes when InitializeRow fires for the first time. This means that the e.ReInitialze property can't be used to restrict my logic to only running the first time. The second time the InitializeRow event fires for all the rows is when I know the editable checkbox columns, but ReInitialize is already true.
I don't know at design time what the name of the checkbox columns will be because this is a user control that can be bound to many different datatypes. Also, I seem to only have this issue what a grid already has data and new data is being bound to it. The initialzeRow events fire as soon as the BindingSource's data source is changed. I can't find an event that catches that in the grid.
gianca said:Hi, I dont know if you issue is fixed but I have the same problem and i use the property e.Row.IsActiveRow in InitializeRow Event ... this property is false when the event InitializeLayout is called, and is true when the row is editing.
That's not true. This will only return true when the row being initialized is active. So it will only ever be true for a single row, if any row at all, and there is no guarantee that there will ever be an ActiveRow.
Hi, I dont know if you issue is fixed but I have the same problem and i use the property e.Row.IsActiveRow in InitializeRow Event ... this property is false when the event InitializeLayout is called, and is true when the row is editing.