Child -> Parent relationship

T

Trygve Lorentzen

Hi,

how can I get a value from a parent table to show up in a textfield (and be
updatable) based on the corresponding foreign key in the child table. The
two tables have a DataRelation object between them made by the designer i
Visual Studio (so it should be correctly set up)?

An Example:

Addresses (FK: CountryCode, PostalCode) <- PostalAdr (PK: CountryCode,
PostalCode)

I create a new Address row and then type in CountryCode and PostalCode, then
details from PostalAdr table should be looked up and displayed in its bound
textfields. I also want to be able to add new PostalAdr rows if it doesn't
already exist and also edit the values in the PostalAdr row that is looked
up.

Say I have 3 textfields; txtCountryCode, txtPostalCode, txtPostalAddress.

Which DataTable should each of these be bound to? Does the DataRelation make
the lookup automatically or do I need to set the RowFilter for the PostalAdr
DataView?

All help will be greatly appreciated!!!

Sincerely,
Trygve
 
W

William Ryan eMVP

<<> how can I get a value from a parent table to show up in a textfield (and
be
updatable) based on the corresponding foreign key in the child table.>>

That can be qutie tricky since the child data appears based on the parent
records. So in order to do this, you'd need to respond to different events.
I'll address this in a second:

<< two tables have a DataRelation object between them made by the designer i
Visual Studio (so it should be correctly set up)? It doesn't really matter
where you do it, in VS.NET or through code, it should work both ways.

PostalCode)

I create a new Address row and then type in CountryCode and PostalCode,
then
details from PostalAdr table should be looked up and displayed in its bound
textfields. I also want to be able to add new PostalAdr rows if it doesn't
already exist and also edit the values in the PostalAdr row that is
lookedup.Say I have 3 textfields; txtCountryCode, txtPostalCode,
txtPostalAddress. >>

You can add a new create a new datarow based on the child row... DataRow dro
= ChildTable.NewRow();

Now, all you've done is created a new row based on the CHild table schema,
you haven't added it yet. If you do ChildTable.Rows.Add(dro) and you have
null values where the aren't allowed, or you add a countrycode, postalcode
combo that doesn't exist in the parent, it will blow up immediately b/c it
violates the constraint. Typically, you can use the GetParentRow method of
the DataRow to determine its parent rows. This can/is done irrespective of
whether or not the fields are bound. Here's an example of how to do this
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
frlrfsystemdatadatarowclassgetparentrowstopic.asp . If you had a hashtable
for instance (there are a lot of ways to do this, I'm trying to explain it
conceptually) of the row position of each PK value in a datatable, you could
set the bindingContext position to that value, using the PK to find it in
the hashtable. You could use something like myBindingContext.Position =
HashTable.Item(ValueOfPK) With a composite key this will be a little
trickier but you can combine them to do the find. Also, you can do a SElect
on the primary key, grab the values and set the BindingContext's position
that way http://www.knowdotnet.com/articles/adopartiii.html

As far as which table to bind to, you'll want to you need to understand the
difference between simple and complex binding.

http://www.informit.com/articles/article.asp?p=29663

TextBoxes have simple binding, that is, they only have one bound value at a
time. ComboBoxes, DataGrids, and LIstBoxes have complext binding meaning
that you can show only one value but you can have multiple ones in there as
a lookup table. If you only have TextBoxes, this is not going to be a fun
thing to do..typically you'll use a complex bound source for lookup fields
(which are often the parent fields like in a Zip Code lookup. Each record
you iterate through will have a Zip Code in a given field, but it may be
linked to a parent with each unique zip code in it. So you'd use a combo
box here so they could display the individual record, but select any of the
acceptable values). On the other hand, you'll often use complex binding to
show child records. If I had a bunch of textboxes with my basic Account
information in them, a listbox on the side with all of the possible
customers, then I may have a grid on the bottom with all of my transaction
history. So when I click on the listbox with my name, the textboxes will
show my information like FirstName, LastName etc but the details grid will
have many values in it from each of my transactions. each time a new record
is selected (by changing the BindingContext's position) new parent info
fills the parent text boxes and the grid is filled with the child info.

How you set up Complex binding can depend on a lot of things. Remember that
most complex bound controls have a display member and a value member.
Display is what you see, Valuemember is the actual selectedvalue. Anyway,
you might bind to the parent datatable for this, or the child datatable, it
really depends on your situation.

You can do all of this with a Dataview and manipulate the rowfilter like you
mention. All you need to do is bind your controls to the DataView and when
you set the filter the data will change. You could even do this without
using a BindingCOntext although I probably wouldn't do it that way. I think
the whole of your problem can be solved with the bindingContext but if you
are going to add to the child table, and the value may or may not exist in
the parent, you are going to have some trouble. YOu can call the AddNew
Method of the BindignContext, but that adds a new parent record and
effectively clears everything out. Since there's no value in the given
parent record, the child controls are cleared out too although once the
parent is created you can add to the child records. I might have some non
bound text boxes with a button, and then when the user clicks the button,
check to see if the parent record exists, if so, you don't need to do
anything but move the BindingContext to its position. If it doesn't add it
and move to its position as well).

Check out the article on complex binding b/c I think that will get you
through this. And let me know if you have any problems..I know this is a
lot here...If you can post the actual code you have that would probably help
too.

Bill
 

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