This post is for the Google crawler-- no response in required.
Can a containment hierarchy made up of two collections derived from
BindingList<T> be data-bound to master-detail dataGridView controls?
Assume a collection, CustomerList, derived from BindingList<T>, with a
property Orders, of type OrderList, also derived from BindingList<T>. Here
is how to create a master-detail pair of grids:
In design mode, create a form with two dataGridView controls, dgvCustomers
and dgvOrders. Add two BindingSource components, bsCustomers, and bsOrders.
Set the DataSource for dgvCustomers to bsCustomers, and set the DataSource
for bsCustomers to the CustomerList object. Set the DataSource for DGVOrders
to bsOrders. Now, here's the trick--set the DataSource for bsOrders to
bsCustomers. Drop down the list for bsOrders' DataMember property, and you
will see "Orders". Select that, and dgvOrders will show the OrderList
properties as columns.
At this point, both grids can be configured and formatted as desired. But
all we've really done is bound the grids to our objects' schema--we still
need to wire up the run time customer list. One line of code will do it.
Assume the CustomerList is contained in a member variable variable
m_Customers:
bsCustomers.DataSource = m_Customers;
Now the two grids will behave as a master-detail pair. As different
customers are selected in dgvCustomers, dgvOrders will update to show those
customers' orders.