ComboBox BindingContext

G

Guest

I have 2 ComboBoxes on a form bound to the same DataSet using a New
BindingContext for the second one. Both ComboBoxes need to have their
SelectedValue bound to a different DataSet . The first one works properly,
but the second one will not select any value. I am pretty sure that this has
something to do with the New BindingContext used on the second ComboBox, but
I can't get around it.

Note: The ComboBoxes are supposed to show a list of Employees (children).
And the SelectedValue is from the Contacts table. Each Contact record has 2
Employee_ID fields.

Any help would be greatly appreciated!!

With ComboBox1
.DataSource = ds2
.DisplayMember = "Initials"
.ValueMember = "PrimaryKey"
End With
ComboBox1.DataBindings.Add("SelectedValue", ds1, "ChildPrimaryKey1")

With ComboBox2
.BindingContext = New BindingContext
.DataSource = ds2
.DisplayMember = "Initials"
.ValueMember = "PrimaryKey"
End With
ComboBox2.DataBindings.Add("SelectedValue", ds1, "ChildPrimaryKey2")
 
B

Bart Mermuys

Hi,

Aspnot said:
I have 2 ComboBoxes on a form bound to the same DataSet using a New
BindingContext for the second one. Both ComboBoxes need to have their
SelectedValue bound to a different DataSet . The first one works
properly,
but the second one will not select any value. I am pretty sure that this
has
something to do with the New BindingContext used on the second ComboBox,
but
I can't get around it.

Note: The ComboBoxes are supposed to show a list of Employees (children).
And the SelectedValue is from the Contacts table. Each Contact record has
2
Employee_ID fields.

Any help would be greatly appreciated!!

With ComboBox1
.DataSource = ds2
.DisplayMember = "Initials"
.ValueMember = "PrimaryKey"
End With
ComboBox1.DataBindings.Add("SelectedValue", ds1,
"ChildPrimaryKey1")

With ComboBox2
.BindingContext = New BindingContext
.DataSource = ds2
.DisplayMember = "Initials"
.ValueMember = "PrimaryKey"
End With
ComboBox2.DataBindings.Add("SelectedValue", ds1,
"ChildPrimaryKey2")

There is some confusion here, you say you bind to DataSet's presumably ds1
and ds2, but you don't specify any table names when binding, so did you
leave them out or did you really bind to DataTable's ?

And there is a problem with the new BindingContext, it doesn't only apply
for DataSource, but also for the DataBindings, this wil happen:

Form's BindingContext (which ComboBox1 uses) contains :
- CurrencyManager1 for Employee (ok)
- CurrencyManager1 for Contact (ok)

New BindingContext (which ComboBox2 uses) contains :
- CurrencyManager2 for Employee (ok)
- CurrencyManager2 for Contact ( BAD )

You'll not only end up with two CurrencyManagers for Employee, but also for
Contact, which are not in sync and you only want one for Contact.


*Instead* of using a different BindingContext, you must use a different
DataView for each ComboBox:

ComboBox1.DataSource = _
new DataView( ds2.Tables("Employee") )
....
ComboBox2.DataSource = _
new DataView( ds2.Tables("Employee") )
...

HTH,
Greetings
 
G

Guest

Sorry. In my attempt to simplify the code I posted, I did forget the tables.
Your solution looks like just what I need. I will give it a try in the
morning.

Thanks for the assistance!!
 

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