0

As the title suggests, I'm having an issue using a filter with an ICollectionView.

Basically I have a TextBox. When ever I type a letter into this TextBox, The filter is triggered (Thanks to INotifyPropertyChanged). As a result, the content the ICollectionView displays is narrowed down to match what's been typed. Or rather, it narrows down the content displayed in what's databound to the ICollectionVIew. i.e ListBox, DataGrid...

Below is a property for a TextBox that utilises the filter is bound to.

    // This is a property bound to a TextBox meant to Search the First Name of a person
    private string _searchFirstName
    public string SearchFirstName
    {
        get
        {
            return _searchFirstName;
        }
        set
        {
            _searchFirstName= value;
            OnPropertyChanged("SearchFirstName");
            // After any changes have been made, the filter is fired.
            PrincipalView.Filter = PrincipalFilter;
        }
    }

Then comes the Filter that's fired by the property.

    private bool PrincipalFilter(object item)
    {
        // principal is the name of the table back in my relational database (MySQL)
        principal principal = item as principal;
        return principal.FirstName.Contains(SearchFirstName)
    }

You see the above works fine for the simple reason being that everybody has a first name. As thus with every record / row entered within the table principal, there will never be a null or empty cell in the FirstName column. However, the same cannot be said for a column for second name, or WorkPhone.

So when I add a "SearchSecondName" property for instance, where not everyone in the table has a second name (ie. some of the cells in the actual corresponding table column are empty), I get a NullReferenceException thrown every time in the actual filter. So I cannot filter out the ICollectionView based on those fields, even if it's necessary.

I would really like to be able to filter information using these columns but can't seem to figure out a way around it. Any assistance on getting around this would be greatly appreciated.

Offer
  • 630
  • 1
  • 9
  • 26

1 Answers1

0

In response to your comment, you simply need to null check SecondName before trying to call a method on it:

return principal.SecondName != null && principal.SecondName.Contains(SearchSecondName);

I will now close this question as a duplicate.

Adam Houldsworth
  • 60,104
  • 9
  • 137
  • 177
  • Thanks for the answer. That definitely works. There's a problem though. I intended to use this filter with "SecondName" in conjunction with "FirstName" and "Surname". So if I was to Search using the FirstName textbox for instance, it would only, return results that also had "SecondName" populated, and it would ignore all the others which are empty. I was hoping that a solution could be reached where it could still return the empty cells without throwing the exception. – Offer May 19 '15 at 09:13
  • @offer I simply return false on null, but you could always return true. – Adam Houldsworth May 19 '15 at 10:18