I have a tree in Grid view, bound to a list (which is a custom type derived from BindingList) of objects (which implement INotifyPropertyChanged). One of the columns is of type CheckState (set in code, since the Designer doesn't know about that type), and is bound to an object property which is declared as CheckState. All of these properties default to unchecked.
Here's the behavior: if I check the checkbox on one item, nothing happens (that is, the property on the object does not get set). If I now check the checkbox on a second item, the property on the *first* item is set, but not the property on the second item. If I check a third item, the second item will get set, and so forth. When the property is modified, I am firing the PropertyNotified event like I'm supposed to.
This is pretty clearly something wrong with my code. Any idea what I could be doing to cause this behavior?
Thanks, Aaron
Hi Aaron,
I don't see any reason why that behavior would change. But if you want to be safe, you could create an editor and assign it to the column and then hook the ValueChanged on each one separately. Or assign the same editor that you created to both column and hook the event once. Either way, this would mean you are no longer relying on the default editor in the column and you would be immune to any future changes in functionality. :)
OK, that's fine - as long as the behavior isn't going to change in the next version, there's no problem. Thanks for all your help.
Aaron
My guess is that you are using the same editor for both columns (which is perfectly valid) and so if you hook the editor on each column, you are really hooking the same editor twice.
If you are not assigning an Editor or EditorComponent to the column, then the tree creates one internally and it will re-use the same one for multiple columns.
OK, several things were going on here. First, yes, I was hooking the event more than once (that is, my code to hook the event was getting executed multiple times); fixing that reduced the calls from four to two. I'm hooking the event on two different column sets, so I tried disabling one of them and that brings it down to one, and in fact the event continues to get hit even if I click the checkbox in the other column set.
This makes me nervous, because my guess is that it's working by coincidence. Is the editor control shared between column sets? Can I rely on that continuing to be the case forever?
My only guess is that you are hooking the event more than once. I hooked mine in Form_Load so it only gets hooked once, and I only got the event once for each click on a checkbox.
Put a breakpoint on the line of code where you are hooking the event and see if it's getting hit mode than once.
If that's not the case, then I don't know why that's happening. Maybe you could put a breakpoint in the event handler and look at the call stack to see where the event is being fired from each time.