繁体   English   中英

即使数据更新成功,ComboBox也会在数据绑定更新后变为空白

[英]ComboBox goes blank after data bound update, even though data updates successfully

我有一个问题,当数据绑定时让ComboBox按预期运行,我不知道问题出在哪里。

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

所有这些似乎都能正常工作,除了ComboBox在更新值后变为空白,这是我不明白的。

当“Selection”属性的数据类型从Int32更改为字符串时,它完全按预期工作。 但是,作为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"));
        }
    }
}

它并没有让我感到沮丧。 使用SelectedValue绑定,它将尝试在每个(即CodeOrLabel )的ValueMember和绑定属性( Selection )之间找到匹配(意味着Equals )。 但是(例如) "123".Equals(123) 永远不会是真的,所以这永远不会匹配。 所以可能是因为决定没有选定的项目(因为没有匹配)。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM