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.

About these ads

Actions

Information

4 responses

16 05 2009
Sami

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

16 05 2009
Sami

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

if(String.IsNullOrEmpty(m_FilterString))
{
((IBindingListView) this).RemoveFilter();
return;
}

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 ruan.moita@gmail.com please? Thanks, Ruan

22 06 2011
Andrew

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)
{
m_OriginalCollection.AddRange(this);
}
List currentCollection = new List(this);
Clear();

foreach (T item in currentCollection)
{
// …
}

to this:

if (m_OriginalCollection.Count == 0)
{
m_OriginalCollection.AddRange(this);
}
Clear();

foreach (T item in m_OriginalCollection)
{
// …
}

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s




Follow

Get every new post delivered to your Inbox.

%d bloggers like this: