Combo Box Data Binding Question

J

Joe Keller

Hello,

How do I go about data binding a combo box to two different tables? One
table for the lookup values and the other table for where the value of the
combo box is stored?

In other words, I want to populate the combo box with values from table A
but when I save the database record, I want table B to hold the value of
what is showing in the combo box?

Thanks,

Joe
 
A

Alex Feinman [MVP]

Do you mean something like this:
Table States (ID int, DisplayAs nvarchar(2))
Table Addresses (ID int, ...., StateID int, ...)

You want the combobox show the list of states, but to update the StateID
field in the Addresses table.

To do this set combobox datasource to the table States and add the binding -
SelectedValue to the StateID field in the table Addresses:
DataTable tblStates = new DataTable()
... fill the table
DataTable tblAddresses = new DataTable()
... fill the table

combo1.DataSource = tblStates;
combo1.ValueMember = "ID";
combo1.DisplayMember = "DisplayAs";
combo1.DataBindings.Add("SelectedIndex", tblAddresses, "StateID");
 
D

DarkBls

I have a problem about LookUp Table. I have done one like yours:

cBRapport.DataSource=re;
cBRapport.DisplayMember="Meaning";
cBRapport.ValueMember="Index";
with re a struct[].

I can change the value inserted in the database by changing the
selected value, but when I'm changing the context position, the combo
doesn't display the database value (i.e
SelectedValue=SelectedItem=null and SelectedIndex=-1)
Furthermore it doesn't fire any exception (not null field in the
database) !

I'm lost.
 
A

Alex Feinman [MVP]

I believe you also need to create a data binding. Add
cBRapport.Bindings.Add("SelectedValue", <form data source>, <field name>)

DarkBls said:
I have a problem about LookUp Table. I have done one like yours:

cBRapport.DataSource=re;
cBRapport.DisplayMember="Meaning";
cBRapport.ValueMember="Index";
with re a struct[].

I can change the value inserted in the database by changing the
selected value, but when I'm changing the context position, the combo
doesn't display the database value (i.e
SelectedValue=SelectedItem=null and SelectedIndex=-1)
Furthermore it doesn't fire any exception (not null field in the
database) !

I'm lost.

"Alex Feinman [MVP]" <[email protected]> wrote in message
Do you mean something like this:
Table States (ID int, DisplayAs nvarchar(2))
Table Addresses (ID int, ...., StateID int, ...)

You want the combobox show the list of states, but to update the StateID
field in the Addresses table.

To do this set combobox datasource to the table States and add the binding -
SelectedValue to the StateID field in the table Addresses:
DataTable tblStates = new DataTable()
.. fill the table
DataTable tblAddresses = new DataTable()
.. fill the table

combo1.DataSource = tblStates;
combo1.ValueMember = "ID";
combo1.DisplayMember = "DisplayAs";
combo1.DataBindings.Add("SelectedIndex", tblAddresses, "StateID");
 

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