Populating combobox with items and values from arrays

Posted on 2007-08-02
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
    LVL 96

    Expert Comment

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

    WinForms ComboBox has DisplayMember and ValueMember.


    Author Comment

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

    LVL 96

    Accepted Solution

    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

    Very good.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Article by: Ivo
    C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
    Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    779 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

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now