ComboBox will not update its display list unless you first change the selection

Update: I checked the answer before fully testing it until it works. I updated the code below, so you just need to paste it into an empty WinForms project and compile it.

UPDATE: I found that if I change the selected item in ComboBox to any other item, it will now behave as expected (in my code below I would switch from test1 to test2). Since I have not received any answers yet, I am changing the question to this question.

Why do I need to go to another item in the combo box before it shows the changes I made to the underlying data source?

Here is a quick example of what is happening.

  • Change test1to test1asdftext in txtBroken
  • click to commit changes.
  • the text in the combo box is not updated.
  • Change combo box for test2
  • change test2to test2asdftext in txtBroken
  • click to commit changes.
  • the text in the combo box immediately shows that "test2asdf" still displays test1for the first item in the drop-down list
  • change to test1
  • displays a combo box test1.test1asdf
  • update text box to test1asd In combo box
  • only displayed test1asd

Besides, that behind the scenes changes the selected item to download and changes it (it looks like such a hack), how can I fix it?


BindingSource, List<Holder>, Holder.Name . , Holder.Name, , , . , , . ?

using System;
using System.ComponentModel;
using System.Windows.Forms;

namespace Sandbox_Form
{
    public class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            lstBroken = new BindingList<Holder>();
            lstBroken.Add(new Holder("test1"));
            lstBroken.Add(new Holder("test2"));
            bsBroken = new BindingSource(lstBroken, null);
            cmbBroken.DataSource = bsBroken;
            cmbBroken.DisplayMember = "Name";
            cmbBroken.SelectedIndex = 0;
            txtBroken.DataBindings.Add("Text", bsBroken, "Name");
            txtBroken.TextChanged += new EventHandler(txtBroken_TextChanged);

        }

        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }

        void txtBroken_TextChanged(object sender, EventArgs e)
        {
            ((Control)sender).FindForm().Validate();
        }
        private BindingSource bsBroken;
        private BindingList<Holder> lstBroken;
        private ComboBox cmbBroken;
        private TextBox txtBroken;
        private Label label1;
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.cmbBroken = new System.Windows.Forms.ComboBox();
            this.txtBroken = new System.Windows.Forms.TextBox();
            this.label1 = new System.Windows.Forms.Label();
            this.SuspendLayout();
            // 
            // cmbBroken
            // 
            this.cmbBroken.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.cmbBroken.FormattingEnabled = true;
            this.cmbBroken.Location = new System.Drawing.Point(12, 32);
            this.cmbBroken.Name = "cmbBroken";
            this.cmbBroken.Size = new System.Drawing.Size(94, 21);
            this.cmbBroken.TabIndex = 0;
            // 
            // txtBroken
            // 
            this.txtBroken.Location = new System.Drawing.Point(13, 60);
            this.txtBroken.Name = "txtBroken";
            this.txtBroken.Size = new System.Drawing.Size(93, 20);
            this.txtBroken.TabIndex = 1;
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(13, 13);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(41, 13);
            this.label1.TabIndex = 2;
            this.label1.Text = "Broken";
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(284, 262);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.txtBroken);
            this.Controls.Add(this.cmbBroken);
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private void cmbWorks_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
    public class Holder
    {
        public Holder(string name)
        {
            Name = name;
        }
        private string _Name;
        public string Name
        {
            get { return _Name; }
            set
            {
                _Name = value;
            }
        }
    }
}

List<String> Holder.Name, ( , , , ). , , , , . Observable .

+5
1

BindingList List. . Dinesh Chandnani, .NET Client, :

BindingList<T> - IBindingList, ListChanged, /// ... . bindSource , , "" thos this BindingSource.

, , , .

BindingList<Holder>, , . . BindingSource DataSource , null dataMember .

, , :

public partial class Form1 : Form
{
    private BindingSource bs;
    private BindingList<Holder> bList;

    public Form1()
    {
        InitializeComponent();

        bList = new BindingList<Holder>();
        bList.Add(new Holder("test1"));
        bList.Add(new Holder("test2"));

        bs = new BindingSource();
        bs.DataSource = bList;

        cmb.DataSource = bs;
        cmb.DisplayMember = "Name";
        cmb.ValueMember = "Name";

        // updates when focus leaves the textbox
        txt.DataBindings.Add("Text", bs, "Name");

        // updates when the property changes
        //txt.DataBindings.Add("Text", bs, "Name", false, DataSourceUpdateMode.OnPropertyChanged);
    }
}

txt , DataSourceUpdateMode.OnPropertyChanged .

BindingList:

1) bsBroken = new BindingSource(lstBroken, null); :

bsBroken = new BindingSource();
bsBroken.DataSource = lstBroken;

: bsBroken = new BindingSource() { DataSource = lstBroken };

( ). Do , dataMember, null. , .

2). txtBroken_TextChanged. , .

+18

All Articles