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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T13:24:02+00:00 2026-05-28T13:24:02+00:00

I have some logic in the business layer that limits the ComboBox options according

  • 0

I have some logic in the business layer that limits the ComboBox options according to inputs, so I need to change the values in the underlying BindingList. But when the list gets changed, the two-way binding becomes one-way from UI to Entity only.

_mComboBox.DataBindings.Add("SelectedValue", _mEntity, "WifeCount");

Full code with problem in the assign button click handler:

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

namespace EnumDataBinding
{
    public partial class Form1 : Form
    {
        ComboBox _mComboBox = new ComboBox();
        Button _mCheckButton = new Button();
        Button _mAssignButton = new Button();

        BindingList<OptionValue> _mBindingList = new BindingList<OptionValue>();
        List<OptionValue> _mCacheList = new List<OptionValue>();

        Entity _mEntity = new Entity();

        public Form1()
        {
            InitializeComponent();

            // create a reset button
            _mCheckButton.Size = new Size(100, 30);
            _mCheckButton.Text = "Check";
            _mCheckButton.Location = new Point(100, 100);
            _mCheckButton.Click += new EventHandler(_mCheck_Click);

            // create assignment button
            _mAssignButton.Size = new Size(100, 30);
            _mAssignButton.Text = "Assign";
            _mAssignButton.Location = new Point(100, 135);
            _mAssignButton.Click += new EventHandler(_mAssignButton_Click);

            // create a combo box
            _mComboBox = new ComboBox();
            _mComboBox.Size = new System.Drawing.Size(300, 30);
            _mComboBox.Location = new Point(100, 200);

            this.Controls.AddRange(new Control[] {
                _mComboBox,
                _mCheckButton,
                _mAssignButton
            });

            // fill the bindinglist
            _mBindingList.Add(new OptionValue("One", 1M));
            _mBindingList.Add(new OptionValue("Two", 2M));
            _mBindingList.Add(new OptionValue("Three", 3M));

            _mCacheList.Add(new OptionValue("One", 1M));
            _mCacheList.Add(new OptionValue("Two", 2M));
            _mCacheList.Add(new OptionValue("Three", 3M));
        }

        void _mAssignButton_Click(object sender, EventArgs e)
        {
            // reset options
            _mBindingList.Clear();
            foreach (var o in _mCacheList)
                _mBindingList.Add(o);

            // EXPECTED: Update ComboBox.SelectedValue and ComboBox.Text
            // RESULT: Does not happen.
            _mEntity.WifeCount = 3M;

            this.Text = string.Format("SelectedValue: {0}; WifeCount: {1}", _mComboBox.SelectedValue, _mEntity.WifeCount);
        }

        private void PrepareComboBox(ComboBox combobox, BindingList<OptionValue> list)
        {
            combobox.DropDownStyle = ComboBoxStyle.DropDown;
            combobox.AutoCompleteSource = AutoCompleteSource.ListItems;
            combobox.AutoCompleteMode = AutoCompleteMode.Suggest;
            combobox.DataSource = new BindingSource() { DataSource = list };
            combobox.DisplayMember = "Display";
            combobox.ValueMember = "Value";
            combobox.Text = string.Empty;
            combobox.SelectedText = string.Empty;
        }

        protected override void OnLoad(EventArgs e)
        {
            // combo box datasource binding
            PrepareComboBox(_mComboBox, _mBindingList);

            // entity data binding
            _mComboBox.DataBindings.Add("SelectedValue", _mEntity, "WifeCount", false);

            base.OnLoad(e);
        }

        void _mCheck_Click(object sender, EventArgs e)
        {
            this.Text = string.Format("SelectedValue: {0}; WifeCount: {1}", _mComboBox.SelectedValue, _mEntity.WifeCount);
        }
    }

    public class Entity : INotifyPropertyChanged
    {
        decimal _mWifeCount;

        public decimal WifeCount { get { return _mWifeCount; } set { _mWifeCount = value; OnPropertyChanged("WifeCount"); } }

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    public class OptionValue
    {
        string _mDisplay;
        object _mValue;

        public string Display { get { return _mDisplay; } set { _mDisplay = value; } }

        public object Value { get { return _mValue; } set { _mValue = value; } }

        public OptionValue(string display, object value)
        {
            _mDisplay = display;
            _mValue = value;
        }
    }
}

Update: Adding an event handler to the ComboBox seems to work:

void _mComboBox_SelectedValueChanged(object sender, EventArgs e)
{
      var binding = (sender as Control).DataBindings["SelectedValue"];
      if (binding != null)
          binding.WriteValue();

      this.Text = string.Format("SelectedValue: {0}; WifeCount: {1}", _mComboBox.SelectedValue, _mEntity.WifeCount);
}
  • 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-28T13:24:03+00:00Added an answer on May 28, 2026 at 1:24 pm

    I believe that in order to have two-way binding, you need to implement the INotifyPropertyChanged interface for the element that is in the binding list. The reason is that the BindingList that’s being used as the datasource doesn’t know when any of the elements have changed unless the elements pass on that information. However, it can still pass on events relating to items being added and removed (assuming you specify AllowRemove/AllowNew properties to true) because that event is in the realm of the list, not the individual elements.

    Edit: Bah! Jumped the gun and didn’t read the question/problem thoroughly. Here’s the issue, adding databindings apparently defaults to a one way binding (initial bind value ONLY). What you need to do is specify the DataSourceUpdateMode when adding the databinding to the combobox:

    _mComboBox.DataBindings.Add("SelectedValue", _mEntity, "WifeCount", false, DataSourceUpdateMode.OnPropertyChanged);
    

    Just tested this out with everything else staying the same and it worked. Let me know!

    Edit: So it isn’t working (I wasn’t clearing the list), and I figured out why. So this is what I’m noticing. For some reason, the binding context for the entity is getting cleared anytime the underlying datasource is getting changed. Not entirely sure why, but I have most definitely found that that’s the issue. The way I found out was adding a watch to the binding context of the _mComboBox for the entity: _mComboBox.BindingContext[_mEntity] and keeping track of the Bindings count. As soon as a new item gets added to the _mBindingList, it seems to muck with the internal databindings of the ComboBox, which ultimately drops the binding for the Entity.WifeCount -> ComboBox.SelectedValue binding we setup. Tried various things, but I’m not entirely sure WHY the PropertyManager drops the binding when the underlying datasource changes.

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

Sidebar

Related Questions

I am creating a series of UserControls that all have some similar business logic.
I have a library that I created with some business logic that includes writing
Let's say you have a business logic method that can perform some operation across
I have a business layer that has some business objects/POCOs/entities/whatever. I also have some
I have some logic in a method that operates on a specified type and
I have some logic that depends upon two properties being set, as it executes
In XCode 4.2, I have some logic tests that I want to exercise against
I have some custom logic that needs to be executed every single time a
I have a HtmlHelper extension method that I would like to apply some logic
Good people of SO, Today I have some serious concerns on my business layer

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.