2

So I've found a lot of questions similar to this tho nothing really solve my problem..

I have a combobx that is bounded by a datasource

cmbProduct.DataSource = this.masterDataSet.Product.Where(x => x.Location == getLocation).AsDataView();
                    cmbProduct.DisplayMember = "Product";
                    cmbProduct.ValueMember = "Product";

But whenever i update the source, the combobox items does not update automatically. I still need to close then reopen the form.

is there a method to refresh/reload/or update the combobox?

Sinatr
  • 20,892
  • 15
  • 90
  • 319
Katherine
  • 319
  • 1
  • 4
  • 15
  • 1
    The data source would need to be observable and fire off an event (notification) when it was updated. There is already a nice library for this called Reactive Extensions (Rx Extensions). – Stephen Brickner Nov 20 '15 at 10:20

2 Answers2

1

You could implement an event that fires whenever the DataSet changes. The event could reset the Datasource and rebind it.

Somewhere in your code:

yourDataController.DataChanged += OnDataChanged; and the implementation

public void OnDataChanged(object sender, EventArgs e)
{
    cmbProduct.Items.Clear();
    cmbProduct.DataSource = this.masterDataSet.Product.Where(x => x.Location ==     getLocation).AsDataView();
    cmbProduct.DisplayMember = "Product";
    cmbProduct.ValueMember = "Product";
}

Edit: Of course you need to manually implement the event and cause it to fire every time your data changes.

Nicolas Dias
  • 641
  • 11
  • 22
leonard_deutsch
  • 263
  • 2
  • 16
  • Hi thanks for ur answer. Im not really familiar with DataController, what is it exactly? – Katherine Nov 20 '15 at 11:01
  • Your data controller is simply your data accessing layer. I assume you have some logic that edits or updates your dataset. That is where the event needs to go. – leonard_deutsch Nov 20 '15 at 11:04
1

Solution 1

You can use an implementation of IBindingList as DataSource to view changes of data source in the bound list control (complex two-way data binding). The most suitable implementation is System.ComponentModel.BindingList<T>.

Then when you add items to the binding list, or remove item from it you will see changes immediately in the control.

Solution 2

But as a more simple solution with less changes for you, you can reset the databinding of your cmbProduct this way when you need; for example after a change, call RefreshBindings();:

public void RefreshBindings()
{
    var list =  put your updated list here;

    this.cmbProduct.DataSource = null;
    this.cmbProduct.DataSource = list;
    this.cmbProduct.DisplayMember = "set the display member here";
    this.cmbProduct.ValueMember = "set the value member here";
}
Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
  • Hi! Thanks for ur solutions. The second solution does not seem to work, the list only becomes empty and was not populated by the updated list. I'm new at C# and not really familiar with the binding list.Can u provide a simple sample code for that? Thank you :) – Katherine Nov 20 '15 at 10:58
  • So I think the second solution is more suitable for you. – Reza Aghaei Nov 20 '15 at 11:01
  • I Copied your code and if your code works, this code should work. for example in my code `getLocation` is not defined and you should change the code a little to make it work. If it shows an empty list it means that the result of query (value of list) is empty. so check the query. – Reza Aghaei Nov 20 '15 at 11:02
  • You can check the edited code and put the values that mentioned in the code. – Reza Aghaei Nov 20 '15 at 11:08
  • [Here](http://stackoverflow.com/a/33625054/3110834) is another of my answers about binding list. Hope you find this and that answer hepful:) – Reza Aghaei Nov 20 '15 at 11:12
  • @Katherine [Accepting Answers: How does it work?](http://meta.stackexchange.com/a/5235/308647) - While you can only accept one answer, you can vote for as many answer as you find helpful :) – Reza Aghaei Dec 06 '15 at 12:27