C# 数据绑定更新后,组合框变为空白,即使数据更新成功

C# 数据绑定更新后,组合框变为空白,即使数据更新成功,c#,winforms,data-binding,combobox,C#,Winforms,Data Binding,Combobox,当数据绑定时,让组合框按预期运行有问题,我不确定问题出在哪里 在下面的代码中,将创建一个组合框,并提供一个值的数据绑定列表,然后将数据绑定到表单。这个想法是,组合框应该显示选项列表,当选择一个选项时,它应该更新数据源,并在状态文本框中显示 所有这些似乎都正常工作,除了更新值后组合框变为空白,这是我不理解的 当“Selection”属性的数据类型从Int32更改为string时,它的工作方式与预期完全相同。但是,与Int32一样,该框变为空白,即使它正确设置了值 如能帮助您了解如何解决此问题,将不

当数据绑定时,让组合框按预期运行有问题,我不确定问题出在哪里

在下面的代码中,将创建一个组合框,并提供一个值的数据绑定列表,然后将数据绑定到表单。这个想法是,组合框应该显示选项列表,当选择一个选项时,它应该更新数据源,并在状态文本框中显示

所有这些似乎都正常工作,除了更新值后组合框变为空白,这是我不理解的

当“Selection”属性的数据类型从Int32更改为string时,它的工作方式与预期完全相同。但是,与Int32一样,该框变为空白,即使它正确设置了值

如能帮助您了解如何解决此问题,将不胜感激

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"));
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Windows.Forms;
命名空间ComboxDataBindingTest
{
公共部分类Form1:Form
{
组合框;
文本框状态;
ValuesDataSource _bsValues;
BindingSource\bsu对象;
绑定(u绑定);;
内部选择;
公共表格1()
{
_选择=0;
_bsValues=新值DataSource();
_bsObject=newbindingsource();
_bsObject.DataSource=这个;
_状态=新文本框();
_状态。左=20;
_排名前50名;
_状态。宽度=200;
_status.ReadOnly=true;
_box=新组合框();
_box.Name=“box”;
_框。左=20;
_box.Top=20;
_框宽=200;
_box.DropDownStyle=ComboBoxStyle.DropDownList;
_box.ValueMember=“CodeOrLabel”;
_box.DisplayMember=“标签”;
_box.DataSource=\bsu值;
_绑定=_box.DataBindings.Add(“SelectedValue”,“Selection”);
this.Controls.Add(_框);
this.Controls.Add(_status);
}
公共整数选择
{
获取{return\u selection;}
设置{u selection=value;_status.Text=value.ToString();}
}
}
公共阶级价值
{
私有字符串_code=null;
私有字符串_label=“”;
公共值(字符串代码、字符串标签)
{
_代码=代码;
_标签=标签;
}
公共字符串代码
{
获取{return\u code;}
}
公共字符串标签
{
获取{return\u label;}
}
公共字符串编码器标签
{
获取{return\u code==null?\u标签:\u code;}
}
}
公共类值DataSource:BindingList
{
公共价值数据源()
{
增加(新价值(“1”、“加利福尼亚”);
增加(新价值(“2”、“内华达”);
增加(新价值(“3”、“亚利桑那”);
增加(新价值(“4”,“俄勒冈州”);
}
}
}

我并不惊讶它在挣扎。使用
SelectedValue
绑定,它将尝试在每个
ValueMember
(即
CodeOrLabel
)的
ValueMember和绑定属性(
Selection
)之间找到匹配项(意思是
等于
)。但(例如)
“123”。等于(123)
,这永远都不是真的,所以这永远不会匹配。因此,它可能决定不存在所选项目(因为没有匹配项)


基本上,在这里使用
字符串
,或者在整个过程中使用
int

谢谢大家!!我知道这一定是一个类型问题,但我找不到它。通过将CodeOrLabel的别名设置为CodeInt32并将其用作ValueMember来解决。这允许我使用相同的值对象,即使绑定到不同数据类型的属性时也是如此。