Filtering with BindingList

S

sjoshi

Hello All

I'm trying to use a filter with collectiong derived off BindingList
and I see that if I use the foreach way of adding and checking the
item then I get instant feedback on the grid. If I use a Predicate to
filter using FindAll then I see my grid refreshed only when I change
the selected row.

My class is defined as this ....

public class BindingListView<T> : BindingList<T>, IBindingListView

Basically I'm getting the criteria and then doing this in
UpdateFilter:

int equalPos = m_FilterString.IndexOf('=');
string propName = m_FilterString.Substring(0, equalPos).Trim();
string criteria = m_FilterString.Substring(equalPos + 1,
m_FilterString.Length - equalPos - 1).Trim();
criteria = criteria.Substring(1, criteria.Length - 2); // string
leading and trailing quotes

//Get a property descriptor for the filter property
PropertyDescriptor propDesc = TypeDescriptor.GetProperties(typeof(T))
[propName];
if (m_OriginalColl.Count == 0)
m_OriginalColl.AddRange(this);

//Make a List object from current collection
List<T> currentColl = new List<T>(this);
Clear();

/*** This commented part gives instant feedback on the grid
//foreach (T item in currentColl)
//{
// object val = propDesc.GetValue(item);
// if (val.ToString().Trim() == criteria)
// {
// Add(item);
// }
//}

//*** This needs manual changes to row to update the grid


((List<T>)Items).AddRange(currentColl.FindAll(GetPredicateDelegate(propDesc,
criteria)));

Where this returns the predicate

private Predicate<T> GetPredicateDelegate(PropertyDescriptor prop,
object key)
{
Predicate<T> pred = delegate(T item)
{
if (prop.GetValue(item).Equals(key))
return true;
else
return false;
};
return pred;
}

Any hints are appreciated.

thanks
Sunit
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top