Link to home
Start Free TrialLog in
Avatar of smaguire
smaguireFlag for Canada

asked on

Populating combobox with items and values from arrays

Hi,
I am trying to populate a combobox from 2 string arrays, the first array contains the name of the item and te second one contains the value of the item:
string[] RangeName;
string[] RangeValue;

RangeName = (new string[] { "Uni10Volts", "Uni5Volts", "Uni2Pt5Volts", "Uni2Volts"});
RangeValue = (new string[] { "100", "101", "102","103"});

so, when the user selects i.e Uni10Volts from the combobox, I want to be able to retrieve the value 100 when using the SelectedValue property. what do I need to add to the following code:
int i;
for (i = 0; i < RangeName.Length; i++)
   {
        this.cmbScanRange.Items.Add(RangeName[i]);  //This will add the array to the combobox

        // what do I need to add here
   }

It was really easy in vb with the DataFieldValue and DataFieldText but I don't have such an options.

does anyone know how to accomplish this task?

thanks
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Are you talking about WinForms or ASP.NET?

WinForms ComboBox has DisplayMember and ValueMember.

Bob
Avatar of smaguire

ASKER

WinForms ComboBox
I tried DisplayMember and ValueMember but I was getting always the last value added to the combobox

ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Very good.

thanks