Fill a ComboBox Using Look up Table

LeonardoSigales

New member
Joined
Mar 11, 2009
Messages
3
Programming Experience
1-3
Hi

I'm using Visual Basic 2008 Express to make a program for Fill and retrieve some data for a printed form. In the main program Window I have several comboboxs linked to some Lookup tables and Detail tables. The tables are like this:

*Clients:
**ID_Clients
**Name
**ID_MaritalStatus 8-------- \
\
*MaritalStatus \
**ID_MaritalStadis <F-------
**DES_MaritalStatus

The combobox DES_MaritalStatus Its Linked to the Column on the lookup table MaritalEstatus. So when I run the Form the mentioned ComboBox show the Value "Married" When it reads "1" From ID_Clients From Clients For the Client Jon Smith. So far so good.

The actual problem Begin here. I need the combobox to display all the Data contain in the look up table. If i click in the drop down list i just can see the Descripcion for the marital status of the displayed Record from the Clients table. I need the combo to list all the Marital status so the user can change it when need it.


On short, ¿How can i do to make a datalinked combobox show in the dropdown list all the option for the selected lookup table, but display as firs option the description related to the ID on the Details Table?

Thanks in advance.
 
You have to bind the ComboBox twice: once to the parent table and once to the child table. To populate the drop-down list you use complex data-binding. You fill a DataTable with the contents of your MaritalStatus table and then bind it to the ComboBox like so:
VB.NET:
myComboBox.DisplayMember = "DES_MaritalStatus"
myComboBox.ValueMember = "ID_MaritalStatus"
myComboBox.DataSource = myDataTable 'or myBindingSource
You then use simple data-binding to bind your Clients table to all your controls, including the ComboBox:
VB.NET:
myComboBox.DataBindings.Add("SelectedValue", myDataTable, "ID_MaritalStatus") 'or myBindingSource
 
Thanks for the lead man

Ok So I put this
VB.NET:
myComboBox.DisplayMember = "DES_MaritalStatus"
myComboBox.ValueMember = "ID_MaritalStatus"
myComboBox.DataSource = myDataTable 'or myBindingSource
myComboBox.DataBindings.Add("SelectedValue", myDataTable, "ID_MaritalStatus")
mytextBox.DataBindings.Add("SelectedValue", myDataTable, "ClientName")
in the Load event of the window and I have to deactivate in the control properties the binding attributes, or the VB throw me the duplication binding exception.

Doing these I’m able to see to name of the client His actual MaritalStatus and the rest of the status when i click the dropdown list. But the BindingNavigator doesn’t works after that.

So I delete these lines
VB.NET:
myComboBox.DataBindings.Add("SelectedValue", myDataTable, "ID_MaritalStatus")
mytextBox.DataBindings.Add("SelectedValue", myDataTable, "ClientName")

and restore de databindings properties of the controls from the properties windows. And Now the Binding navigator works, the combo show all the options and almost everything its fine.

Just 3 questions:
1) How do I save the changes made in the registers? I was trying to modify the code from the Save button of the Binding navigator whit no success.

2) These binding navigator it’s a problematic control? Should i build my own navigator or it’s trustworthy?

3)the workaround I use, based on your help, it’s a right one or it’s just a patch?

Thanks in avance
 
There's no specific need for you to remove the bindings in the designer and then add them in code. The two are equivalent, so you can use either. You could have simply edited the bindings in the designer. I had to show you the code option because, of course, I can't post designer options here, but you can do the equivalent binding in the designer.

To save data you need to call Update on your DataAdapter or TableAdapter. From VS 2008 you can also call UpdateAll on your TableAdapterManager, which will save every table in a typed DataSet.

The BindingNavigator is fine. The BindingNavigator is associated with a BindingSource and you are now binding your controls to the DataTable directly instead of to the BindingSource. Note that, in my EXAMPLE code I indicated that you could bind to either. If you're already binding to a BindingSource then you should continue to do so.

Your solution is basically right but, as I said, you should have simply tweaked the existing bindings in the designer, not discarded those and redone it all in code.
 
Thanks

Really thanks for your help all its working fine now. But Im sure I will have many questions in the future. Also I hope I can help other users in the forum.
 

Latest posts

Back
Top