Simultaneous Data Binding and Adding Objects

One of the new features in version 2.5.0 is support of binding to one or multiple data sources with Grid.DataSource  property with simultaneous adding of objects with Grid.Rows.Add() / Row.Add() methods.

This feature enables creation of almost any data hierarchy. If an IBindingList data source is bound to the grid, the grid subscribes to events of this list automatically adding, emoving or deleting rows. If child rows are added to the row with Grid.Rows.Add() / Row.Add(), these rows will be correctly deleted upon relevant notification from IBindingList.ListChanged.


//Basket class
public class Basket
{
//Private fields
private readonly BindingList<Order> _orders = new BindingList<Order>();

//Public properties
public IList<Order> Orders
{
get { return _orders; }
}
}

//Initialize the grid
public void InitializeGrid(Grid grid, IList<Basket> baskets)
{
//Bind the grid to a basket collection
grid.DataSource = baskets;

//Add orders to each basket row
foreach(Basket basket in baskets)
{
Row row = grid.DataObjects.FindFirstRow(basket);
if(row != null)
{
foreach(Order order in basket.Orders)
{
row.Add(order);
}
}
}

//Add some other data object on the topmost hierarchical level
grid.Rows.Add(new SomeOtherData());
}

Back to .Net Grid Features