Heirarchical DataBinding in Windows Forms.

J

jehugaleahsa

Hello:

I am sure this is a common need, so I hope I can be brief.

I have a heirarchical database setup such that one customer can have
multiple contracts and those contracts can have multiple child
records, and so on. I would like very much to be able to move from one
customer to the other and for my code to automatically retrieve the
associated contracts and repopulate the Form.

Before I venture on writing some code of my own, I wanted to make sure
something like this doesn't already exist.

If it doesn't exist, I plan on writing an derived BindingSource that
recieves a parent BindingSource and calls an event handler to detect
when Current changes (the PositionChanged event). The event handler
will be responsible for populating the underlying DataSource of the
child BindingSource.

From this, I will be able to isolate the repopulation from the
interface. However, I have to deal with events such as the parent
BindingSource being cleared. That is why I was hoping to escape the
confusion and use an existing code set.

Let me know whether I should start writing this code or not. If there
is a Form control or property that does this, that would be nice to
know about too.

Thanks,
Travis
 
S

sloan

Infragistics has a grid, which allow Hierarchal data, if I recall correctly.

It cost$, but may be worth it to see what they have.

I think their distribution license is easy to deal with.
 
R

RobinS

You can use two grids, and bind one to a foreign key. Here's an example
using strongly typed datasets from Brian Noyes' book on data binding:

-----Master/Details-----

--Set DataSource on binding source to dataset containing both.
--Set DataMember on binding source to parent table.
--Set DataSource properties on grids to respective binding sources.
--To hook up the child grid, set DataSource property on the
child binding source to the parent binding source. Then
specify the name of the data relation as the DataMember.

*****
CustomersDataSet ds = CustomersDataSet.GetCustomers();
m_CustomersBindingSource.DataSource = ds;
m_CustomersBindingSource.DataMember = "Customers";
m_CustomersGrid.DataSource = m_CustomersBindingSource;

m_ChildOrdersBindingSource.DataSource = m_CustomersBindingSource;
m_ChildOrdersBindingSource.DataMember = "FK_Orders_Customers"; //foreign key

m_OrdersGrid.DataSource = m_ChildOrdersBindingSource;

*****

So assuming you have two grids, one for the parent (customers) and
one for the child (orders), I believe when you change records in
the parent, the children change automatically.

You don't have to use a grid, you could bind anything to the datasets;
this just gives you an idea how to set up the bindings.

RobinS.
GoldMail, Inc.
 
J

jehugaleahsa

You can use two grids, and bind one to a foreign key. Here's an example
using strongly typed datasets from Brian Noyes' book on data binding:

-----Master/Details-----

--Set DataSource on binding source to dataset containing both.
--Set DataMember on binding source to parent table.
--Set DataSource properties on grids to respective binding sources.
--To hook up the child grid, set DataSource property on the
  child binding source to the parent binding source. Then
  specify the name of the data relation as the DataMember.

*****
CustomersDataSet ds = CustomersDataSet.GetCustomers();
m_CustomersBindingSource.DataSource = ds;
m_CustomersBindingSource.DataMember = "Customers";
m_CustomersGrid.DataSource = m_CustomersBindingSource;

m_ChildOrdersBindingSource.DataSource = m_CustomersBindingSource;
m_ChildOrdersBindingSource.DataMember = "FK_Orders_Customers"; //foreignkey

m_OrdersGrid.DataSource = m_ChildOrdersBindingSource;

*****

So assuming you have two grids, one for the parent (customers) and
one for the child (orders), I believe when you change records in
the parent, the children change automatically.

You don't have to use a grid, you could bind anything to the datasets;
this just gives you an idea how to set up the bindings.

RobinS.
GoldMail, Inc.
------------------------------------------













- Show quoted text -

Is there a way to do this with a BindingList<T> of business objects?
 
R

RobinS

I've been thinking about this. I definitely think it can be done. This is my
first guess without actually coding it myself.

First I would define a List<child> in the parent class that is populated
when the parent is populated. I am assuming, of course, that there aren't
millions. If there are a lot of children for each parent, you will want to
use a separate class, and load it only when you need it.

Next I would define a list<parent> class, and a object data source pointing
to that list. (Do you know how to set that up?) This needs to inherit
BindingList<parent>.

Then you can databind the object data source for the parent and children to
two grids.

Give this a try, if you can't get anywhere, post back and I will try it this
weekend and provide more specific help.

RobinS.
GoldMail, Inc.
-------------------------
You can use two grids, and bind one to a foreign key. Here's an example
using strongly typed datasets from Brian Noyes' book on data binding:

-----Master/Details-----

--Set DataSource on binding source to dataset containing both.
--Set DataMember on binding source to parent table.
--Set DataSource properties on grids to respective binding sources.
--To hook up the child grid, set DataSource property on the
child binding source to the parent binding source. Then
specify the name of the data relation as the DataMember.

*****
CustomersDataSet ds = CustomersDataSet.GetCustomers();
m_CustomersBindingSource.DataSource = ds;
m_CustomersBindingSource.DataMember = "Customers";
m_CustomersGrid.DataSource = m_CustomersBindingSource;

m_ChildOrdersBindingSource.DataSource = m_CustomersBindingSource;
m_ChildOrdersBindingSource.DataMember = "FK_Orders_Customers"; //foreign
key

m_OrdersGrid.DataSource = m_ChildOrdersBindingSource;

*****

So assuming you have two grids, one for the parent (customers) and
one for the child (orders), I believe when you change records in
the parent, the children change automatically.

You don't have to use a grid, you could bind anything to the datasets;
this just gives you an idea how to set up the bindings.

RobinS.
GoldMail, Inc.
------------------------------------------













- Show quoted text -

Is there a way to do this with a BindingList<T> of business objects?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top