I have a table with a column containing a single character representing what type of animal described by the record. I have a comboBox which has the Items property filled with the descriptions corresponding to each code. Example "A" = Amphibian, "C" = Coldwater fish... and so on).

What I need to ComboBox to do is display the description corresponding to the current code, when the user changes the selection, the underlying code character should be altered accordingly. Do I need to do this through code, or is there a way to set up a relationship using the ValueMember and DisplayMember properties?

If the value being mapped is an integer instead of a character, can it be done then?

Yes to both questions.

Bind the Combobox to the table or a bindingsource, and set the DisplayMember to what you want shown in the Text property and the ValueMember to the key data.

Now if you want to do this as an unbound control, then you will need to use some code.
IOW, if you do not want the combobox items to be populated by the database, but rather just use the items list you provided in the IDE it takes a bit more work. There are several approaches to consider. First the ugly way, means creating a method that will interpret the selected item index or Text value and return the desired value. Another method is to set the item index based on the data column value. Some programmers create an array or generic collection to supply the means of interpretation (value to text / text to value).

A slightly more elegant method is to create a class that allows you to emulate the operation of a bound control. Since the combobox item is of type Object you can stick just about anything you want in there, however it must supply as a minimum a ToString() method that will return the information to display as Text.

public class itemobject
        private string Text;
        private int value;
        public itemobject(string Text, int value)
            this.Text = Text;
            this.value = value;
        public override string ToString()
            return Text;
        public int Value { get { return value; } }

Now you can initialize your combobox with an object that can supply a large number of properties and/or methods.

Lets say you have the following data you want to populate the combobox.

MyCombo.Items.Add( new itemObject("California",1) );
Same for the other states.

Now you have to update the control anytime the underlying table row position changes. You can use an event of the Bindingsource or some other control that detects when the user has changed the current row position of the data (such as a DataGridView).

You will need to set the SelectedIndex of the combobox so it displays the correct text value.
I use a generic FindValue method for this operation:

private int FindValue(ComboBox cb, int id)
                for (int i = 0; i < cb.Items.Count; i++)
                    itemobject obj = (itemobject)cb.Items[i];
                    if (obj.Value == id)
                        return i;
            catch { }
            return -1;

When the data row changes, I use the BindingSource.PositionChanged event to set my combobox selectedindex property:

// abreviated event code
 MyCombo.SelectedIndex = FindValue(MyCombo, (int)data["StateID"]);

When you want to retrieve the current value from the combobox, you can use this:

itemobject obj = (itemobject)MyCombo.SelectedItem; 
if( obj != null )
    return obj.value;
This article has been dead for over six months. Start a new discussion instead.