Click here to Skip to main content
15,887,350 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am in trouble while working on Databinding in windows form. I have two classes, one is Project and another is Update. Now all project object is having a list of Updates and it is binded to a combobox, but when the user changes selection need to display/bind the properties of Update object to another controls. But is not updating as expected, when the user changes selection. Please help me on this.

What I have tried:

public class Project
{
    private int _id;
    private string _name;

    public Project(int id, string name)
    {
        _id = id;
        _name = name;

        ReadUpdates();
    }

    public List<Update> AvailableUpdates { get; set; }
    public int Id { get { return _id; } }

    public string ProjName
    {
        get { return _name; }
        set { _name = value; }
    }

    private void ReadUpdates()
    {
        AvailableUpdates = new List<Update>();

        for (int i = 0; i < 10; i++)
        {
            AvailableUpdates.Add(new
                Update(i, DateTime.Now.AddDays(i)));
        }
    }
}

public class Update
{
    private string _title;
    private int _uid;
    private DateTime _updatedOn;

    public Update(int id, DateTime updatedOn)
    {
        _title = $"Update:{id}";
        _uid = id;
        _updatedOn = updatedOn;
    }

    public string Title
    {
        get { return _title; }
        set { _title = value; }
    }

    public int UId
    {
        get { return _uid; }
        set { _uid = value; }
    }

    public DateTime UpdatedOn
    {
        get { return _updatedOn; }
        set { _updatedOn = value; }
    }
}

public partial class Form1 : Form
{
    private Update _currentUpdate;
    private Project _project;

    public Form1()
    {
        InitializeComponent();

        _project = new Project(1, "Sample Project");

        DoBindings();
    }

    private void DoBindings()
    {
        NameBox.DataBindings.Add("Text", _project, "ProjName");
        IdBox.DataBindings.Add("Text", _project, "Id");

        UpdatesCombo.DataSource = _project.AvailableUpdates;
        UpdatesCombo.DisplayMember = "UId";

        _currentUpdate = (Update)UpdatesCombo.SelectedItem;

        UpdateTitle.DataBindings.Add("Text", _currentUpdate, "Title");
        UpdateDate.DataBindings.Add("Value", _currentUpdate, "UpdatedOn");
    }

    private void UpdatesCombo_SelectionChangeCommitted(object sender, System.EventArgs e)
    {
        _currentUpdate = (Update)UpdatesCombo.SelectedItem;
    }
}
Posted
Updated 19-Jul-18 0:26am

1 solution

use a BindingList<t> instead of List<t> and a BindingSource and it will work as desired

using System.ComponentModel;

public partial class Form1 : Form
  {
    private Update _currentUpdate;
    private Project _project;
    private BindingSource bs;
    ...
    private void DoBindings()
    {
      NameBox.DataBindings.Add("Text", _project, "ProjName");
      IdBox.DataBindings.Add("Text", _project, "Id");

      bs = new BindingSource();
      bs.DataSource = _project.AvailableUpdates;
      UpdatesCombo.DataSource = bs;
      UpdatesCombo.DisplayMember = "UId";
      UpdatesCombo.ValueMember = "UId";

      _currentUpdate = (Update)UpdatesCombo.SelectedItem;

      UpdateTitle.DataBindings.Add("Text", bs, "Title");
      UpdateDate.DataBindings.Add("Value", bs, "UpdatedOn");
    }

  }


  public class Project 
  {
     //...
     public BindingList<Update> AvailableUpdates { get; set; }

  }

  //...
  private void ReadUpdates()
      {
        AvailableUpdates = new BindingList<Update>();

        for (int i = 0; i < 10; i++)
        {
          AvailableUpdates.Add(new
              Update(i, DateTime.Now.AddDays(i)));
        }
      }


you will find a more detailed answer to a quite similar question here:
c# - ComboBox will not update its display list unless you change selections first - Stack Overflow[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900