Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8697479
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T01:32:12+00:00 2026-06-13T01:32:12+00:00

I have a WinForms application. I’ve populated my ComboBox with the following code: cboGridSize.Items.Clear();

  • 0

I have a WinForms application. I’ve populated my ComboBox with the following code:

cboGridSize.Items.Clear();
for (int i = 2; i <= 12; i++)
    cboGridSize.Items.Add(new KeyValuePair<string,int>(i.ToString(), i));
cboGridSize.SelectedValue = 4;

However, the last line has absolutely no effect. The ComboBox appears with no items selected.

So I was doing some debugging and noticed some odd things. The following image is from the watch window after setting cboGridSize.SelectedIndex to 0.

Watch Window http://www.softcircuits.com/Client/debugwin.jpg

Even though the SelectedItem property contains exactly what I would expect, SelectedValue is still null. Although the documentation for SelectedValue is pathetic, I understood it would contain the value of the selected item (SelectedItem). Instead, the two properties seem completely unrelated. Can anyone see what I have wrong?

As you can see, I have the ValueMember property set. And the DropDownStyle property is set to DropDownList.


EDIT:

Once Nikolay Khil set me straight on the issue here (why the docs for SelectedValue don’t do that escapes me), I decided to simply write my own code to accomplish the same task. I’m posting it here in case anyone is interested.

static class ComboBoxHelper
{
    public static void LookupAndSetValue(this ComboBox combobox, object value)
    {
        if (combobox.Items.Count > 0)
        {
            for (int i = 0; i < combobox.Items.Count; i++)
            {
                object item = combobox.Items[i];
                object thisValue = item.GetType().GetProperty(combobox.ValueMember).GetValue(item);
                if (thisValue != null && thisValue.Equals(value))
                {
                    combobox.SelectedIndex = i;
                    return;
                }
            }
            // Select first item if requested item was not found
            combobox.SelectedIndex = 0;
        }
    }
}

This is implemented as an extension method so I simply change my original code as follows:

cboGridSize.Items.Clear();
for (int i = 2; i <= 12; i++)
    cboGridSize.Items.Add(new KeyValuePair<string,int>(i.ToString(), i));
cboGridSize.LookupAndSetValue(4);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T01:32:13+00:00Added an answer on June 13, 2026 at 1:32 am

    Both ValueMember and DisplayMember properties are used only if DataSource property is defined.

    So, you should re-write your code as follows:

    private readonly BindingList<KeyValuePair<string, int>> m_items =
        new BindingList<KeyValuePair<string, int>>();
    
    public YourForm()
    {
        InitializeComponent();
    
        cboGridSize.DisplayMember = "Key";
        cboGridSize.ValueMember = "Value";
        cboGridSize.DataSource = m_items;
    
        for (int i = 2; i <= 12; i++)
            m_items.Add(new KeyValuePair<string,int>(i.ToString(), i));
    
        cboGridSize.SelectedValue = 4;
    }
    

    Links:

    • BindingList class
    • ObservableCollection class
    • INotifyCollectionChanged Interface
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a WinForms application, and wanted to add some nice WPF controls, one
I have a winforms application and I'm not following any kind of design pattern.
I have a winforms application that is doing the following: on each query: Db
I have a Winforms application (written in C#) that is deployed on a network
I have a winforms application and I am trying to create a method that
I have a winforms Application, On Close button click, I am hiding that application
I have a WinForms application, with login form, and I want to store the
I have a WinForms application (WF), and a Library called by that WinForms application
I have a Winforms application that generates its own PrintDocument object for printing. It
We have a winforms application calling a stored procedure every few seconds. The stored

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.