DataGrid Filter Method Very Slow I'm having a lot of trouble in my Database program with trying to implement an effective DataGrid filtering method. After taking advice from a previous code review I'm trying to implement a DataModel method of coding. My previous method of searching my DataGrid was this;

SearchGrid (PREVIOUS)

private void SearchGrid(object sender, TextChangedEventArgs e)
    {
        DataView dv = dataGrid.ItemsSource as DataView;
        if (compNameRad.IsChecked == true)
        {
            dv.RowFilter = "CompanyName LIKE '%" + searchBox.Text + "%'";
        }
        if (compTownRad.IsChecked == true)
        {
            dv.RowFilter = "CompanyTown LIKE '%" + searchBox.Text + "%'";
        }
        if (compPcodeRad.IsChecked == true)
        {
            dv.RowFilter = "CompanyPcode LIKE '%" + searchBox.Text + "%'";
        }
    }


This worked fine with the way I bound my DataGrid before. I now bind my DataGrid using a Model class and the previous filter method does not work. I have written a new filter method which is this;

SearchGrid (NEW)

private void SearchGrid(object sender, TextChangedEventArgs e)
        {
            if (!string.IsNullOrEmpty(searchBox.Text))
            {
                ICollectionView view =      CollectionViewSource.GetDefaultView(dataGrid.ItemsSource);
                view.Filter += (obj) =&gt;
                {
                    CompanyModel model = obj as CompanyModel;
                    if (model == null)
                        return true;

                    if (compNameRad.IsChecked == true)
                    {
                        return !string.IsNullOrEmpty(model.CompanyName) &amp;&amp; model.CompanyName.Contains(searchBox.Text);
                    }
                    if (compTownRad.IsChecked == true)
                    {
                        return !string.IsNullOrEmpty(model.CompanyTown) &amp;&amp; model.CompanyTown.Contains(searchBox.Text);
                     }
                    if (compPcodeRad.IsChecked == true)
                    {
                        return !string.IsNullOrEmpty(model.CompanyPcode) &amp;&amp; model.CompanyPcode.Contains(searchBox.Text);
                    }
                    return false;
                };
            }
        }


Now this does work OK, however there are two issues:


is that it is extremely slow when I type text to search the DataGrid, and 
it doesn't deal with the most simple of cases, for example if the name of the Company is "A1" then searching for "a1" will not find the company.


I have been recommended to use Entity Framework however as I am using an OleDB connection (this is a very old Database I am rewriting) I have been unable to get this to work.

Is there a more efficient search method I can use that is quicker and would also find all companies (i.e case insensitive)?
private void SearchGrid(object sender, TextChangedEventArgs e)
    {
        if (!string.IsNullOrEmpty(searchBox.Text))
        {
            ICollectionView view =      CollectionViewSource.GetDefaultView(dataGrid.ItemsSource);
            view.Filter += (obj) =&gt;
            {
                CompanyModel model = obj as CompanyModel;
                if (model == null)
                    return true;

                if (compNameRad.IsChecked == true)
                {
                    return !string.IsNullOrEmpty(model.CompanyName) &amp;&amp; model.CompanyName.Contains(searchBox.Text);
                }
                if (compTownRad.IsChecked == true)
                {
                    return !string.IsNullOrEmpty(model.CompanyTown) &amp;&amp; model.CompanyTown.Contains(searchBox.Text);
                }
                if (compPcodeRad.IsChecked == true)
                {
                    return !string.IsNullOrEmpty(model.CompanyPcode) &amp;&amp; model.CompanyPcode.Contains(searchBox.Text);
                }
                return false;
            };
        }
    }  



The problem with your code is that each time the text changed you are adding a new Filter to the ICollectionView by using  

view.Filter += (obj) =&gt;  


this little + is doing all the harm. For each raise of the TextChanged event a new filter is added and never removed.  

If you remove the + you will see that your filtering will become faster.  



The problem with A1 vs a1 is that the Contains() method is case sensitive. A better way would be to use IndexOf(string, StringComparison) like so  

return !string.IsNullOrEmpty(model.CompanyName)  
       &amp;&amp; model.CompanyName.IndexOf(searchBox.Text, StringComparison.OrdinalIgnoreCase) &gt; -1;