Hi,
I have a recursive data structure which I am binding to the grid. I have a class "Patient" which Implements INotifyPropertyChanged and has another property public BindingList<Patient> Children. Which can have recursive objects. I am creating BindingList<Patient> to bind to the grid. Everything works fine until I try to add another child at run time to a row which does not have anything in "Children" Property. Nothing happens. I am attaching a sample can some one please let me know what I am doing wrong here?
The sample you posted here is not a recursive data source. There is no Children property on your Patient object. I also don't see anything in this sample that would allow me to add a row to the grid, the datasource or any child data.
Did you post the wrong sample?
My guess is that your Children property was returning Null instead of an empty collection - that would explain why you can't add child rows. But that's just a guess.
Hi Mike,Sorry for the goof up. I attached the sample again. Your guess is somewhat right initially the Children property will have null value. But before adding an object to children collection I am initializing it and adding it. Wouldn't that return the collection? Please refer the sample. Thanks
Returning null from a bound list property like this is a bad idea. It's going to cause all sorts of problems. Making the property settable is not a good idea, either.
The problem with the null value is that you are assuming that the bound controls and the BindingManager are not accessing this property until you set it to something. That's not necessarily correct. The grid is going to try to create the bands as soon as it's bound and it's going to ask the BindingManager for the data structure (columns) for each level of the data. If the data source returns null, the BindingManager may not be able to determine the data structure and so the band will not exist or it will exist with no columns.
You should set up your Children property as a lazily created list so that it returns an empty collection and never returns null. I would also remove the setter. I don't think the BindingList/INotifyPropertyChanged setup will be able to handle it if you set the Children property to a new BindingList at some point - although I could be wrong about this.
Thanks Mike. Yes you are right I initialized the list instead of null and now it works fine. Also you are right about the "BindingList/INotifyPropertyChanged setup" it does not work even if I initialized the Children property after binding. So the solution is as you suggested to initialize the Children property instead of returning Null. Thanks for your help.