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 553577
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T11:37:11+00:00 2026-05-13T11:37:11+00:00

I’ve got a problem with getting a ComboBox to operate as expected when data

  • 0

I’ve got a problem with getting a ComboBox to operate as expected when data bound, and I am not sure where the problem is.

In the code below, a ComboBox is created and given a data bound list of values, and is then data bound to the form. The idea is that the ComboBox should display the list of options, and when one is selected, it should update the data source, and be indicated in the status text box.

All of this appears to work correctly, except that the ComboBox goes blank after updating the value, which is what I don’t understand.

When the data type of the “Selection” property is changed from Int32 to string, it works exactly as expected. As Int32, however, the box goes blank even though it sets the value correctly.

Any help understanding how to fix this problem would be appreciated.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ComboboxDatabindingTest
{
    public partial class Form1 : Form
    {
        ComboBox _box;
        TextBox _status;
        ValuesDataSource _bsValues;
        BindingSource _bsObject;
        Binding _binding;
        int _selection;

        public Form1()
        {
            _selection = 0;

            _bsValues = new ValuesDataSource();

            _bsObject = new BindingSource();
            _bsObject.DataSource = this;

            _status = new TextBox();
            _status.Left = 20;
            _status.Top = 50;
            _status.Width = 200;
            _status.ReadOnly = true;

            _box = new ComboBox();
            _box.Name = "box";
            _box.Left = 20;
            _box.Top = 20;
            _box.Width = 200;
            _box.DropDownStyle = ComboBoxStyle.DropDownList;
            _box.ValueMember = "CodeOrLabel";
            _box.DisplayMember = "Label";
            _box.DataSource = _bsValues;
            _binding = _box.DataBindings.Add("SelectedValue", _bsObject, "Selection");

            this.Controls.Add(_box);
            this.Controls.Add(_status);
        }

        public int Selection
        {
            get { return _selection; }
            set { _selection = value; _status.Text = value.ToString(); }
        }
    }

    public class Value
    {
        private string _code = null;
        private string _label = "";

        public Value(string code, string label)
        {
            _code = code;
            _label = label;
        }

        public string Code
        {
            get { return _code; }
        }

        public string Label
        {
            get { return _label; }
        }

        public string CodeOrLabel
        {
            get { return _code == null ? _label : _code; }
        }
    }

    public class ValuesDataSource : BindingList<Value>
    {
        public ValuesDataSource()
        {
            base.Add(new Value("1", "California"));
            base.Add(new Value("2", "Nevada"));
            base.Add(new Value("3", "Arizona"));
            base.Add(new Value("4", "Oregon"));
        }
    }
}
  • 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-05-13T11:37:12+00:00Added an answer on May 13, 2026 at 11:37 am

    It doesn’t surprise me that it is struggling. With a SelectedValue binding, it is going to try and find a match (meaning Equals) between the ValueMember of each (i.e. CodeOrLabel) and the bound property (Selection). But it is never true that (for example) "123".Equals(123), so this will never match. So it is presumably deciding that there isn’t a selected item (since none match).

    Basically, use a string here, or use int throughout.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 385k
  • Answers 385k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Just use following example <script type="text/javascript"> var txtNameID = '<%=… May 14, 2026 at 11:37 pm
  • Editorial Team
    Editorial Team added an answer Did you put "@implementation " at the beginning of your… May 14, 2026 at 11:37 pm
  • Editorial Team
    Editorial Team added an answer Try: var fromRow = document.getElementById("row_1"); while(fromRow !== null) { var… May 14, 2026 at 11:37 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.