ComboBox goes out after updating data, even though the data update was successful

I have a problem with ComboBox working as expected when the data is bound, and I'm not sure where the problem is.

In the code below, a ComboBox is created and a list of values ​​associated with the data is specified, and then the data is attached to the form. The idea is that the ComboBox should display a list of parameters, and when it is selected, it should update the data source and be indicated in the status text box.

All this works correctly, except that the ComboBox is empty after updating the value, which I don’t understand.

When the data type of the Select property changes from Int32 to a string, it works exactly as expected. However, like Int32, the field becomes empty, even if it sets the value correctly.

Any help to understand 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"));
        }
    }
}
+3
source share
1 answer

It does not surprise me that he is fighting. With a binding, SelectedValuehe will try to find a correspondence (value Equals) between ValueMembereach (i.e. CodeOrLabel) and related property ( Selection). But it never happens that (for example) "123".Equals(123), so it will never be consistent. Thus, it is assumed that there is no selected item (since it does not match).

Basically, use stringhere or use intall over.

+2
source

Source: https://habr.com/ru/post/1727930/


All Articles