[ACCEPTED]-Filter a DataGrid on a Text box-wpfdatagrid

Accepted answer
Score: 25

You can filter the Items in the DataGrid 11 by binding it to an ICollectionView that supports filtering.

Details 10 here for .NET 4. The process is the same for 9 .NET 4.5, but it seems the documentation 8 has been lost. There's a small mention to 7 it here under the "Grouping, Sorting, and 6 Filtering" heading.

edit: at the time 5 this was originally written, the WPF toolkit 4 had not been abandoned by Microsoft. The 3 controls that used to be part of it are 2 now in the framework, and the toolkit was 1 alive and doing well here

Score: 7

I have seen at various sites much ado about 3 this matter...

To filter the latter being 2 a datagrid using a datatable as the source, which 1 is quite common to make the code below:

DataTable dt = new DataTable("Table1");

//fill your datatable...

//after fill...
dataGrid1.DataContext = dt;
IBindingListView blv = dt.DefaultView;
blv.Filter = "NAME = 'MOISES'";
Score: 3

There are several solutions, but in my opinion, the 3 best solutions are the ones which uses only 2 DataGrid styles without inventing a new inherited 1 DataGird type. The followings are the best I found:

Score: 0

I have written my own FilterDataGrid Control, it's 11 much more flexible than the ones provided 10 on CodeProject or elsewhere. I can neither 9 post the full code here, nor can I publish 8 it.

But: Since your datasource is most likely 7 wrapped into a ICollectionView, you can 6 do something like this:

    public void ApplyFilters()
    {
        ICollectionView view = CollectionViewSource.GetDefaultView(ItemsSource);
        if (view != null)
        {
            view.Filter = FilterPredicate; 
        }
    }

    private bool FilterPredicate(object item)
    {
        var yourBoundItemOrRow = item as BoundItemType;

        return aFilterObject.Matches(yourBoundItemOrRow);
    }

You can implement 5 any filter logic easily based on this concept. Even 4 very, very powerful filters. Note: I have 3 those methods in my own class derived from 2 datagrid. They can be adapted to work outside 1 of the grid, too, for example in a UserControl

More Related questions