CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 1 of 1
  1. #1
    Join Date
    Jul 2006
    Posts
    2

    ComboBox Display list does not update if a datasource is a boxed string

    I have a combo box databound to a BindingSource bound to a List<Holder> it has Holder.Name as its display value. I also have a text box bound to Holder.Name but if I change the text in the text box it will not change what is displayed in the combo box. Changing selected items and changing back will show the updated text in the text box but will still have the old value displayed in the combo box. How do I make the item in the combo box update?

    Code:
    using System;
    using System.Windows.Forms;
    using System.Collections.ObjectModel;
    using System.Collections.Generic;
    using System.Text;
    
    namespace Sandbox_Form
    {
        public class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                lstBroken = new List<Holder>();
                lstBroken.Add(new Holder("test1"));
                lstBroken.Add(new Holder("test2"));
                bsBroken = new BindingSource(lstBroken, null);
                cmbBroken.DataSource = bsBroken;
                cmbBroken.DisplayMember = "Name";
                txtBroken.DataBindings.Add("Text", bsBroken, "Name");
    
                lstWorks = new List<string>();
                lstWorks.Add("test3");
                lstWorks.Add("test4");
                bsWorks = new BindingSource(lstWorks, null);
                cmbWorks.DataSource = bsWorks;
                txtWorks.DataBindings.Add("Text", bsWorks, null);
            }
            private BindingSource bsBroken;
            private BindingSource bsWorks;
            private List<Holder> lstBroken;
            private List<string> lstWorks;
            private ComboBox cmbBroken;
            private TextBox txtBroken;
            private Label label1;
            private Label label2;
            private TextBox txtWorks;
            private ComboBox cmbWorks;
            /// <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.label2 = new System.Windows.Forms.Label();
                this.txtWorks = new System.Windows.Forms.TextBox();
                this.cmbWorks = new System.Windows.Forms.ComboBox();
                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";
                // 
                // label2
                // 
                this.label2.AutoSize = true;
                this.label2.Location = new System.Drawing.Point(139, 13);
                this.label2.Name = "label2";
                this.label2.Size = new System.Drawing.Size(38, 13);
                this.label2.TabIndex = 5;
                this.label2.Text = "Works";
                // 
                // txtWorks
                // 
                this.txtWorks.Location = new System.Drawing.Point(139, 60);
                this.txtWorks.Name = "txtWorks";
                this.txtWorks.Size = new System.Drawing.Size(93, 20);
                this.txtWorks.TabIndex = 4;
                // 
                // cmbWorks
                // 
                this.cmbWorks.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
                this.cmbWorks.FormattingEnabled = true;
                this.cmbWorks.Location = new System.Drawing.Point(138, 32);
                this.cmbWorks.Name = "cmbWorks";
                this.cmbWorks.Size = new System.Drawing.Size(94, 21);
                this.cmbWorks.TabIndex = 3;
                // 
                // 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.label2);
                this.Controls.Add(this.txtWorks);
                this.Controls.Add(this.cmbWorks);
                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
        }
        public class Holder
        {
            public Holder(string name)
            {
                Name = name;
            }
            public string Name { get; set; }
        }
    }
    UPDATE:
    UPDATE:
    I have found that if I change the selected item on the ComboBox to any other item it now behaves as expected (in my code below I would switch from test1 to test2). As I have not revived any answers yet, I change the question to this.

    Why do I have to change to a different item in the combo box before it will show the changes I make to the underlying data-source?

    Here is a quick test case of what is happening.

    1. Change `test1` to `test1asdf` text in txtBroken
    2. click off to commit change
    3. text in combo box does not update.
    3. Change combo box to test2
    4. change `test2` to `test2asdf` text in txtBroken
    5. click off to commit change
    6. text in combo box immediately shows 'test2asdf' still displays `test1` for first item in the drop-down
    7. change to `test1`
    8. combo box displays `test1` text box displays `test1asdf`
    9. update text box to `test1asd`
    10. combo box immediately displays `test1asd`

    Other than behind the scenes changing the selected item on load and changing it back (this seems like such a hack) how can I fix this?
    Last edited by leftler; August 26th, 2010 at 08:57 AM. Reason: new information

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured