Binding a ComboBox in a Windows Form to a Hierarchical DataSet

G

Greg Dunn

I have two tables in a parent-child relationship. Details on the tables are
as shown below:

Table1
-------
Item_ID (primary key)
Item_Owner (foreign key to Table2)

Table2
-------
Owner_ID (primary key)
Owner_Name


I wish to display, on a form designed for maintenance of the data in Table1,
a combobox which displays owner names (Table2.Owner_Name), but returns as
its bound ValueMember the item owner's ID (Table1.Item_Owner). Can I do
it?

Thanks!
 
M

Miha Markic

Hi Greg,

AFAIK you can't. Binding is against flat table.
However, I am bit confused.
You want to show parent record name and return the child id?
Didn't you mean to display Owner_Name and return Owner_Id (as it is equeal
to Item_Owner)?
 
G

Greg Dunn

Didn't you mean to display Owner_Name and return Owner_Id (as it is equeal
to Item_Owner)?

Since, as you note, the Owner_ID will equal the Item_Owner, it really
doesn't matter which of them is returned. But Table1.Item_Owner is the
column to which I would like the control to be bound.
AFAIK you can't. Binding is against flat table.

I haven't found a way to do it, either. It seems like a nasty limitation,
though, since it forces me a choice among several things, none of which I
want to choose:

1. Foregoing the benefits of data binding in the form, or
2. Not using abstract primary keys, or
3. Denormalizing the database.
 
M

Miha Markic

Hi Greg,

Ah sorry, you are after a lookup, right.
Set ComboBox' ValueMamber and DisplayMember to a column from Table2 and
DataSource to Table2
Go to DataBindings and bind Selected Value to Table1.Item_Owner.

That should enable combobox to act like lookup.
 
C

CJ Taylor

Greg,

You want to fill your Owner table with all the valid vlaues it would be
(complete table scan?) just use a Data Adapter to fill it or whatever you
want.

Then, on your bindings, you want to bind your combo boxes SelectedValue to
your Table1.Item_Owner Property.

This should take care of it.

and on yoru combo box, set your display member to Owner_name and your
valuemember to OwnerID. Thats all it takes.

peace,
cJ
 
G

Greg Dunn

Ah...very easy. Many thanks!

--
Greg Dunn


Miha Markic said:
Hi Greg,

Ah sorry, you are after a lookup, right.
Set ComboBox' ValueMamber and DisplayMember to a column from Table2 and
DataSource to Table2
Go to DataBindings and bind Selected Value to Table1.Item_Owner.

That should enable combobox to act like lookup.
 

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