Filtering on a List<T> BindingSource

13 06 2007

funnel.gifUpdate: 19/06/2007 – I have attached a copy of ListBindingSource.cs which I have modified slightly to match against partial field matches (similar to SQL’s LIKE) rather than matching the whole field.

ListBindingSource.cs 11.5 Kb

I maintain a C# application and a recent feature request was to be able to search a list of items on a number of attributes. The original implementation used a List<T> as the datasource for a Binding Source. I hit a wall when trying to use the Filter property on the binding source.

It turns out that the Filter property depends on the data source implementing IBindingListView (among others) which apparently List does not. I came accross this article that shows how to implement the IBindingListView.

I’ll admit that I don’t fully understand the whole of the code in the listing but I did manage to figure out where the filtering happens. The class is generic in that you can filter on any public property of the objects used to create your list. It also supports filtering on multiple property through successively setting the Filter property.

To use this class you create a BindingListView<T> and use it as the data source for your binding source.

Note: I was only interested in the Filter ability so I have not looked into the implementation of Sort, or how to modify the SortComparer to sort my objects.




9 responses

16 05 2009

Great work man..this is what i was looking for exactly!

16 05 2009

It would be more nice (it wont break if we set Filter property to null or empty string), to start of UpdateFilter() method

((IBindingListView) this).RemoveFilter();

24 06 2010
London Paris Train

I am not able to download this copy. I dont know what the problem is. Can you email it to my email ID please? Thanks, Ruan

22 06 2011

Thank you for posting this code!

I have a slight improvement – I found that on changing the Filter string for the second time, it always returned 0 records.. here’s my attempt at a fix..

in UpdateFilter():

I changed this:

if (m_OriginalCollection.Count == 0)
List currentCollection = new List(this);

foreach (T item in currentCollection)
// …

to this:

if (m_OriginalCollection.Count == 0)

foreach (T item in m_OriginalCollection)
// …

30 09 2015

I am not able to download this copy. I dont know what the problem is. Can you email it to my email ID :

13 10 2016

Post merveilleux. Merci pour le partage…

en attente de plus… 😉 ?

13 05 2017
nas mixtapes

I loved this post! Someone said your blogs fairly often and you’re always developing great stuff.
I shared this on my facebook and my follwers
loved it! Keep up very good work.

19 05 2017
Southern Grace Bed & Breakfast

What’s up mates, its wonderful piece of writing on thе
topic of educationand fuⅼly explained, kᥱep it up all the timᥱ.

12 02 2019

You can send the example to email : . Thank you

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: