I have a grid bound to a collection of objects each object has a collection of child objects and they all display nicely e.g.
object 1
child 1
child 2
object 2
Now in code I add a child to object 2 e.g. MyObject2.Add(child) i then can view the collection at runtime e.g.
((List<MyObject>)myGrid.DataSource)
And this shows that the child has been added succesfully, however if i call myGrid.DataBind() the new child does not appear in the child band in the grid.
How do I get it to appear automatically ??
If I do the following I lose my current layout and positioning but the child is there:
var data = ((List<MyObject>)myGrid.DataSource);
myGrid.DataSource = null;
myGrid.DataSource = data;
Hi,
List<T> is not a very good data source to use for Binding. It doesn't send notification when a new row is added or an existing row is modified.
I recommend using BindingList<T> instead. That will make things better - the BindingList will notify bound controls when new rows are added.
In order to get notification when a field value is changed, you will need to implement INotifyPropertyChanged on MyObject.
Or... if you don't want to (or can't) change your data source, you can refresh the grid manually by calling grid.Rows.Refresh(ReloadData).
BindingList worked a treat I had tried ObservableCollection which didn't appear to work.
In my case I don't need to worry about changes to the properties of the objects its only the relationships I'm building.
Thanks for you help
Barney