I have a Grid which has two viewing modes : All Items, or only items with PropertyX = A
. Views switch based on a CheckBox
toggle.
I have one copy of the collection in my object model layer that is frequently updated, and I'd like to ideally apply the filter to it at the data layer. Is there a way to apply a filter to this collection for binding purposes, without creating a second copy of the collection or applying a UI filter?
I think what I am looking for is something like a CollectionViewSource from WPF, but that doesn't appear to be available in WinForms.
From what I can tell, my options are :
Some data-layer construct that can apply a filter to the binding source that can be modified from the data layer, like a
CollectionViewSource
Create a second copy of my collection, and maintain it in sync whenever the first collection gets updated. Not ideal because of the frequency of items added, removed, and modified.
Use a UI filter... not ideal because there is a lot of UI-specific things done with filters already, so this solution would mix controlling the filter between UI and Data layer, and gets messy.
Something else?
Is there something that acts like a CollectionViewSource in WinForms so I can use option #1? Or if not, what is the standard way of handling this kind of scenario in WinForms?
It should be noted that PropertyX
never changes its value. If it is A, it is always A. So that helps.
Here's what I've been trying to do so far :
public IEnumerable<MyClass> DisplayItems
{
get
{
if (SomeFlagIsChecked)
return _allItems;
return _allItems.Where(p => p.PropertyX == A);
}
}
My issue here is setting the DataSource to IEnumerable doesn't seem to be working. I'm not sure if that's a Winforms thing, a .Net 3.5 thing, or a limitation of the 3rd party control I am using (DevExpress GridView).
I don't want to cast it to a .ToList()
because the collections are anywhere from a few hundred to a few thousand records, and get refreshed every couple of minutes. So ideally I'd like to avoid creating new objects every time I need a refresh.