Click here to Skip to main content
15,893,508 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to use the filter property of ICollectionView object in WPF databinding. Code below -

C#
ICollectionView view = CollectionViewSource.GetDefaultView(GridProductInformation.DataContext);
int supplierID = GetComboBoxSelectedItem();
//Predicate<object> pred = new Predicate<object>(MatchSupplier);
Predicate<object> pred = delegate(object obj)
{
    NorthwindProduct _product = new NorthwindProduct();
    _product = obj as NorthwindProduct;
    int _supplierID = Convert.ToInt16(_product.SupplierID);
    if (_supplierID == supplierID)
        return true;
    else
        return false;
};
view.Filter(pred);

The code throwsa run time exception in the last line - Null reference exception. Object reference not set to instance of an object.

Any idea what is going wrong?
Posted
Updated 1-Oct-10 11:11am
v2

Apologies if you've found the answer already, but this if probably what you need ...
view.Filter = pred;
 
Share this answer
 
v2
@anshudutta their is a casting problem itseems. Try setting a breakpoint at
_product = obj as NorthwindProduct;
I think this is always null.

Evaluate, what
obj 
which is coming inside your predicate.
 
Share this answer
 
Comments
anshudutta 1-Oct-10 0:06am    
I thought that initially, then I commented the code in my predicate and just wrote return True . It still throws the error.
PumbaPumba 1-Oct-10 0:09am    
Do you got any samples for this to re-product. May be I'll give it a try.
anshudutta 1-Oct-10 0:21am    
Well I am binding to a northwind database.. product table and trying to filter products by supplier ids. The combobox is bound to collection of supplier ids. When a particular supplier id is selected, the binding should filter products based on that supplier id. Cant understand why is it throwing Null exception

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900