Databinding Combobox

G

Guest

I want to start using databinding in a new winforms application. I am using
VB.Net 2003. The database has several fields that contain a numeric code
that translates to a text status. For example 1=Open, 2=Closed, etc.

I would like to bind this field to a dropdown combo box. I need the
combobox to display Open, Closed, etc based on the numeric value in the
datarow. Based on their selection, I need the underlying datarow to get a
numeric value returned base on their selection.

Can someone point me to an example of intercepting the bound data before it
gets to a control so that I can change the value and doing the reverse when
it changes in the control.
 
C

Cor Ligthert [MVP]

Danny,

This is very basic in binding to a complex datacontrol what the combobox is.

combobox1.datasource = TheDataTable
combobox1.displaymember = "TheFieldToDisplay" "open etc
combobox1.valuemember = "TheFieldToUse" '1 2 3

You use the index change event to get what is changed.
Be aware that the first time you protect that with a switch or on another
way.
As soon as you set something to a combobox the indexchange event will be
fired (as it will with every control with an index

I hope this gives some idea's

Cor
 
G

Guest

Thank you for the reply. I understand the combobox1.valuemember . This is
the field that contains the numeric value. (1, 2, 3, etc).

Also understand that combobox1.displaymember is the field that should be
displayed. My problem is that the underlying datarow does not have the words
"Open, Closed", etc. It only contains the numeric value.

How do I associate the words "Open, Closed", with the combobox. I need for
the combo box to show "Open", when a 1 is in the database field. I also need
the dropdown list to contain all of the options for this field.
 
G

Guest

After some additional research it looks like I will need to create an
internal table or class/collection that has my options along with the
corresponding numeric value. I will bind the combo box to that table /
collection. I will set the display member to the "text" and the value member
to the "number".

It looks like I will then need to add a binding back to my dataset ---
something like
comboBoxState.DataBindings.Add("SelectedValue",custdataset, "Invoice.Status")

I will give this a try.
 

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