Quantcast
Channel: Windows Forms Data Controls and Databinding forum
Viewing all articles
Browse latest Browse all 2535

ListBox Data Binding Properties not Refreshing

$
0
0

So,

we have three controls. 

ComboBox => have List of ParentObject via data source

ListBox => list of ChildObjects on currently selected ComboBox's ParentObject.

TextBox databound to the current ChildObject of ListBox

Now, using ListBox properties and ONLY listbox properties, how do we determine that the current item has changed when the datasource changes.

Example:

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        protected ParentObject SelectedParent
        {
            get { return comboBox1.SelectedItem as ParentObject; }
        }

        protected ChildObject SelectedChild
        {
            get { return listBox1.SelectedItem as ChildObject; }
        }

        private BindingList<ParentObject> _parents;
        protected BindingList<ParentObject> Parents
        {
            get
            {
                if (_parents == null) _parents = new BindingList<ParentObject>();
                return _parents;
            }
        }

        protected override void OnLoad(EventArgs e)
        {
            this.Parents.Add(new ParentObject() { Name = "Parent 1" });
            this.Parents.Add(new ParentObject() { Name = "Parent 2" });
            this.Parents.Add(new ParentObject() { Name = "Parent 3" });

            this.Parents[0].Children.Add(new ChildObject() { Name = "P1 Child 1" });
            this.Parents[0].Children.Add(new ChildObject() { Name = "P1 Child 2" });
            this.Parents[1].Children.Add(new ChildObject() { Name = "P2 Child 1" });
            this.Parents[1].Children.Add(new ChildObject() { Name = "P2 Child 2" });

            comboBox1.DataSource = this.Parents;
            comboBox1.DisplayMember = "Name";

            base.OnLoad(e);
        }



        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.SelectedParent != null)
            {
                listBox1.DataSource = this.SelectedParent.Children;
                listBox1.DisplayMember ="Name";
            }
            else listBox1.DataSource = null;
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            textBox1.DataBindings.Clear();
            textBox1.Clear();
            if (this.SelectedParent != null)
            {
                textBox1.DataBindings.Add(new Binding("Text", this.SelectedChild, "Name", true, DataSourceUpdateMode.OnPropertyChanged));
            }
        }

        private void listBox1_SelectedValueChanged(object sender, EventArgs e)
        {
            textBox1.DataBindings.Clear();
            textBox1.Clear();
            if (this.SelectedParent != null)
            {
                textBox1.DataBindings.Add(new Binding("Text", this.SelectedChild, "Name", true, DataSourceUpdateMode.OnPropertyChanged));
            }
        }

        private void listBox1_DataSourceChanged(object sender, EventArgs e)
        {
            textBox1.DataBindings.Clear();
            textBox1.Clear();
            if (this.SelectedChild != null)
            {
                textBox1.DataBindings.Add(new Binding("Text", this.SelectedChild, "Name", true, DataSourceUpdateMode.OnPropertyChanged));
            }
        }
    }

    public class ParentObject
    {
        public string Name { get; set; }

        private BindingList<ChildObject> _children;
        public BindingList<ChildObject> Children
        {
            get
            {
                if (_children == null) _children = new BindingList<ChildObject>();
                return _children;
            }
        }
    }

    public class ChildObject
    {
        public string Name { get; set; }
    }

So, Quite simply, using this code (done specifically to highlight the three primary events, SelectedIndexChanged, SelectedValueChanged, DataSourceChanged) switching the combobox from Parent 1 to Parent 2 there is no problem.  When when you select Parent 3:

  • DataSourceChanged is Triggered on listBox1.
  • The Event Method is Triggered, but:
    • SelectedIndex is still > -1
    • SelectedItem is Still Assigned
    • SelectedValue is Still Assigned

  • TextBox databinding is bound to PREVIOUS Datasource's last selected value.

After the DataSource has been changed, then and only then does the SelectedIndex become -1 and the SelectedItem become NULL, but at that point neither the SelectedValueChanged nor the SelectedIndexChanged events are triggered on the list box.  if the SelectedIndex was 1 and is now -1 it has CHANGED but I get no event for it. 

So,how do I detect via the listbox events that there IS no selected item/value anymore, so I can clear the binding. 

Thanks

Jaeden "Sifo Dyas" al'Raec Ruiner




"Never Trust a computer. Your brain is smarter than any micro-chip."
PS - Don't mark answers on other people's questions. There are such things as Vacations and Holidays which may reduce timely activity, and until the person asking the question can test your answer, it is not correct just because you think it is. Marking it correct for them often stops other people from even reading the question and possibly providing the real "correct" answer.


Viewing all articles
Browse latest Browse all 2535

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>