I have a method in my class manager that runs when my backgroundworker is completed and updates the BindingList (_suppliers) and it looks like this:
private void _bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
_suppliers.Clear();
foreach (Classes.Supplier s in (BindingList<Classes.Supplier>)e.Result)
_suppliers.Add(s);
}
public BindingSource BindingSource
{
get
{
if (_suppliers == null)
{
_suppliers = new BindingList<Classes.Supplier>();
}
return new BindingSource(_suppliers, null);
}
}
(The reason I user .Clear() and then loop through and .Add() is because if I user _suppliers = new BindingList<Classes.Supplier>(func.LoadSuppliers());
the BindingList never updates the bound controls... I don't like it)
My problem is that when _suppliers gets modified it changes the selection in all my comboboxes associated with it and that is not wanted.
I bind my comboboxes like this:
public BindingSource BindingSource
{
get
{
if (_suppliers == null)
{
_suppliers = new BindingList<Classes.Supplier>();
}
return new BindingSource(_suppliers, null);
}
}
And I modify them like this:
public bool Add(Classes.Supplier supplier)
{
if (_suppliers == null)
{
_suppliers = new BindingList<Classes.Supplier>();
}
using (SQLiteFunction func = new SQLiteFunction())
{
try
{
if (func.AddSupplier(supplier))
_suppliers.Add(supplier);
}
catch (Exception ex)
{
Helper.ExceptionHandler(ex, "SupplierManager", "Add");
return false;
}
}
return true;
}
Does anyone have any ideas on how to bypass this? Setting .SelectedIndex = -1
or something like that is not wanted either 'cause if a user has selected an item in the lists I want the selection to remain... if it's possible that is.
As always all help and comments are appriciated.
This is expected behaviour. You are deleting the content of your datasource "_suppliers.clear()" during your callback _bw_RunWorkerCompleted. So there is no selected item after that.
To bypass this remember the selected item and assign it after recreation of the list. Maybe you have to implement sth that selchange eventhandler does not get fired.