user5968830
user5968830

Reputation:

Filter ItemsSource

With this code I'm setting the ItemsSource of my datagrid. However I've got more wpf controls that are needed to filter the datagrid, for example from a time range. I could write a new query for this but that seems unnecessary as the data is already available, I just need to filter it. What's the best way to do this?

Any help I could get would be really appreciated!

DateTime dateStart = CalenderSearch.SelectedDates.First();
DateTime dateEnd = CalenderSearch.SelectedDates.Last();

ObjectQuery<Fouten> fouten = eventsEntities.Foutens;

var query =
    (from fout in fouten
    where dateStart <= fout.Datum && dateEnd >= fout.Datum && fout.Rapporten.Treinen.NameTrein == trein.NameTrein
    orderby fout.Datum, fout.Time
    select new
    {
        Datum = fout.Datum,
        Time = fout.Time,
        FoutCode = fout.FoutCode,
        Omschrijving = fout.Omschrijving,
        Teller = fout.Teller,
        Module = fout.Module,
        FoutId = fout.FoutId

    }).AsEnumerable().Select(x => new Fouten
    {
        Datum = x.Datum,
        Time = x.Time,
        FoutCode = x.FoutCode,
        Omschrijving = x.Omschrijving,
        Teller = x.Teller,
        Module = x.Module,
        FoutId = x.FoutId
    }).ToList();

if (query.Count == 0)
    foutensDataGrid.ItemsSource = null;
else
    foutensDataGrid.ItemsSource = query;

Upvotes: 3

Views: 1564

Answers (2)

Vadim Martynov
Vadim Martynov

Reputation: 8902

You can save loaded data in class field and then filter it via handle buttonClicks:

public class YourView
{
    private List<Fouten> loadedData;
    public void LoadData(...)
    {
        ObjectQuery<Fouten> fouten = eventsEntities.Foutens;

        // here you save unfiltered data to the field and then you can use it to filter collection
        loadedData = ...;

        // if you want to filter values immediately you can call filter method right here
        // FilterByFoutCode(someValue);

        if (loadedData.Count == 0)
            foutensDataGrid.ItemsSource = null;
        else
            foutensDataGrid.ItemsSource = loadedData;
    }

    private void FilterByFoutCodeButtonClick(object sender, EventArgs e)
    {
        var filter = FoutCodeTextBox.Content.ToString();
        if(!string.IsNullOrEmpty(filter))
        {
            // if your filter is not empty then filter loadedData by criteria 
            FilterByFoutCode(filter);
        }
    }

    private void FilterByFoutCode(string filter)
    {
        foutensDataGrid.ItemsSource = loadedData.Where(x => x.FoutCode == filter);
    }
}

Also, in the performance context you should not have extra iteration of your collection via select new {...} , .AsEnumerable() and Select(...), just call ToList() immediately after LINQ query.

Finally MVVM pattern is a standard de-facto for WPF it can be useful for your app.

Upvotes: 0

Domysee
Domysee

Reputation: 12854

In WPF, each collection that are used as an ItemsSource in a GUI element have an ICollectionView associated with it.
ICollectionView has a Filter property, which is of type Predicate<object>.

If you set this Filter and call Refresh() afterwards, the DataGrid will update itself to only show the items where Filter returned true.

An example how you could use this:

var collectionView = CollectionViewSource.GetDefaultView(foutensDataGrid.ItemsSource);
collectionView.Filter = o => {
    var fouten = o as Fouten;
    //do your filtering, e.g.
    return fouten.Datum <= dateEnd && fouten.Datum >= dateStart;
}
collectionView.Refresh();

Upvotes: 3

Related Questions