ComboBox not databinding unless selected.

jwh

Well-known member
Joined
Aug 18, 2006
Messages
155
Programming Experience
3-5
I have 2 databound comboboxes on a form.

When an item is selected from the first combobox, a filter is applied to the bindingsource of the second combobox to only show relevant options.

When there is only one option in the second combo box, I want that to be the one that is selected.

I am currently using ComboBox2.Selectedindex=0 to achieve this, and the correct value is shown in the ComboBox2.

However when I try to save the record, I get an error saying that column xxx does not allow nulls... i.e. the second combobox is not databinding the value that it is showing.

If, however, I physically click on the second combobox and select the value (the only one), then it has no problem databinding that value.

All I can assume is that there is another method to select the first item in the combobox in a way that the databinding is happy with.

Can anybody help me?
 
I think i know what you mean,
I can't test it out here but try this:

After this line:
VB.NET:
Expand Collapse Copy
ComboBox2.Selectedindex=0
Add:
VB.NET:
Expand Collapse Copy
'i think this forces the physical click (and the binding) to happen
ComboBox2_SelectedIndexChanged(sender, e) 'If you don't have a sender and e object, use (Me, New System.Eventargs)

Does that do the trick?

Edit:
If it does not: try replacing those 2 lines of code with:

VB.NET:
Expand Collapse Copy
'This is a longshot
ComboBox2.SelectedItem = ComboBox2.Items.Item(0)
 
Last edited:
Thanks for the suggestions, unfortunately neither worked.

I ended up directly setting the selected value in the bindingsource, as described in this post from MSDN:
The most direct way to change values in your BindingSource is to set properties on the current row. I created the following example using the Northwind sample database:
VB.NET:
Expand Collapse Copy
CType(Me.CustomersBindingSource.Current.Row, NorthwindDataSet.CustomersRow).CompanyName = "NewValue"
This example assumes you have created a typed DataSet that contains the Northwind Customers table. If it's not a typed DataSet then you can use the indexer on the row to set a column:
VB.NET:
Expand Collapse Copy
Me.CustomersBindingSource.Current.Row(1) = "NewValue"

 
Odd, I'd have expected 2 comboboxes that were correctly bound to a master:detail pair of related tables and associated datarelation, to work properly.

Fancy posting your project so we can have a look at it?
 
I'm glad the problem is solved but, like cjard said, i would also like to see the project.

What i don't get is that physically clicking the item in the combobox does do the trick, but forcing the selectedindexchanged event does not.

So now I wonder what else vb.net does in the background when an item is physically clicked? I thought it called selectedindexchanged in the background, nothing more?


I presume the binding is set on the selectedvalue of the combobox? not text or selectedtext? (dunno if that matters but it might)
 
Back
Top