Populating combobox with items and values from arrays

Posted on 2007-08-02
Medium Priority
Last Modified: 2012-05-05
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?

Question by:smaguire
  • 2
  • 2
LVL 96

Expert Comment

by:Bob Learned
ID: 19620209
Are you talking about WinForms or ASP.NET?

WinForms ComboBox has DisplayMember and ValueMember.


Author Comment

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

LVL 96

Accepted Solution

Bob Learned earned 2000 total points
ID: 19620316
Try this:

DictionaryEntry[] items = {new DictionaryEntry("Uni10Volts", "100"), new DictionaryEntry("Uni5Volts", "101"), new DictionaryEntry("Uni2Pt5Volts", "102"), new DictionaryEntry("Uni2Volts", "103")};

this.cmbScanRange.DataSource = items;
this.cmbScanRange.DisplayMember = "Key";
this.cmbScanRange.ValueMember = "Value";


Author Comment

ID: 19620435
Very good.


Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Integration Management Part 2
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question