After some deliberation, I found that BindingList
does not support filtering, and the same is technically true for BindingSource
.
So, with standard filtering out of question, I wrote my own implementation, like this:
private class LivingBindingList : BindingList<Producer>
{
public LivingBindingList(List<Producer> source)
: base(source.Where(producer => producer.ChangeTracker.State != ObjectState.Deleted).ToList())
{
rem_cache = source.Where(producer => producer.ChangeTracker.State == ObjectState.Deleted).ToList();
}
List<Producer> rem_cache;
protected override void RemoveItem(int index)
{
this.Items[index].MarkAsDeleted();
this.rem_cache.Add(this.Items[index]);
base.RemoveItem(index);
}
protected override void OnAddingNew(AddingNewEventArgs e)
{
e.NewObject = new Producer()
{
NameProducer = "Новый производитель",
GUID = Guid.NewGuid(),
Type = 1,
Note = String.Empty
};
base.OnAddingNew(e);
}
internal IEnumerable<Producer> GetAllForSubmit()
{
return this.Items.Concat(rem_cache);
}
}
With this, I can bind to the list, add and delete as much as I want, and ofr commit purposes retrieve all records, including dead ones, with GetAllForSubmit().