Grid not updating with insertion of new rows into underlying data source
I have a grid that contains rows with child rows as well. I initially populate the grid by setting my BindingList<Order> to the grid's DataSource.
I need to add additional child rows after the initial binding takes place. Using LINQ, I find the applicable parent row in my BindList<Order> object and add a new child row to the field marked as "HierarchicalField".
However, when I add the new record to my BindingList<Order> object, it does not update the grid, unless I call ResetBindings() on the data source. However, this causes all of the rows to collapse into their parent rows which cannot happen.
I have attached the sample code for your viewing. Please let me know if you have any questions.
Regards,
Dapfor.txt
-
Hello,
Replace the collection of orders in the Order class with BindingList<Order>. In this case the grid can subscribe not only to the root collection but also to a collection, returned by the Order.Orders property. The grid will automatically add/remove rows when this collection is changed. You don't need fire FirePropertyChanged("Orders") if you don't instantiate a new instance of BindingList.Example:
public class Order : INotifyPropertyChanged
{
private readonly IList<Order> _orders = new BindingList<Order>();
...
[Field("Orders")]
[HierarchicalField]
public IList<Order> Orders
{
get
{
return _orders;
}
}
}
Kind regards,
Dapfor0 -
Hi,
Thanks for the quick response. I changed private IList<Order> _orders = new List<Order>();
to private readonly IList<Order> _orders = new BindingList<Order>();
Still no luck. When I executevar row = ds.OfType<Order>().Where(x => x.TicketNumber == "456").First();
row.Orders.Add(new Order("2222", "Another Child row"));
The gird does not show the new child row.
Thanks,
Chris0 -
Hi Chris,
Look at the attached demo project. Each root order contains a BindingList of suborders (of course you can have any number of hierarchies). Add/remove buttons randomly change those lists. Hope, this help you.
Best regards,
Dapfor
HierarchicalFieldDemo.png
HierarchicalFieldDemo.zip0
Please sign in to leave a comment.
Comments
3 comments