I’m trying to get a Winforms combobox to automatically refresh when new rows are written to our database.
POCO EF class:
public class BaseSweep
{
public int BaseSweepId { get; set; }
//stuff removed for clarity
}
I’m binding to the data through a BindingList like this:
public BindingList<BaseSweep> TopSweeps()
{
LocalDbContext.BaseSweep.Load();
return LocalDbContext.BaseSweep.Local.ToBindingList();
}
private void BindSweepList() //called in Form_Load
{
comboBoxSweepIds.DataSource = _dataAccess.TopSweeps();
comboBoxSweepIds.DisplayMember = "BaseSweepId";
comboBoxSweepIds.ValueMember = "BaseSweepId";
}
This works fine for the initial binding, shows the current Ids in the table. As new rows are added to the table, the count in LocalDbContext.BaseSweep.Local goes up as expected. However, comboBoxSweepIds never updates. Any ideas what I am doing wrong?
Mark W led me the right way:
Use a private
BindingList<T>in the form class (_sweepCollection)On the first databind, set up the event handler: