Lucy82
Lucy82

Reputation: 693

Datagrid with ListCollectionView - how to apply filter on all properties without memory leak?

My Datagrid should be filtered by all columns. For that I have a TextBox in my View. So whatever text is being written, Datagrid should filter by it.

I have more questions:

  1. How to apply filter on all columns, based on text?

  2. I tried filtering only one column, but filtering is very slow (aprox. 37.000 items). How can I speed up filtering?

  3. Filtering (tested on one column) produces huge memory leak. Any way to fix this?

Properties for binding:

public ListCollectionView AllData
{
   get { return _alldata; }
   set { _alldata = value; OnPropertyChanged(); }
}
private ListCollectionView _alldata;

public ObservableCollection<DataModel> Data
{
   get { return _data; }
   set { _data = value; OnPropertyChanged(); }
}
private ObservableCollection<DataModel> _data;

Method to get all data (binding):

private async Task Get_Contracts()
{
   Data = await GetData();
        
   AllData = new ListCollectionView(Data);
}

Filtering method:

private void ApplyFilter(bool search_by_text)
{
   try
   {
      if (search_by_text)
      {
         //??  
         AllData.Filter = (e) => {};
    
         //This works - a new Collection created instead of filtering existing one
         //var filter = Data.Where(p => p.GetType().GetProperties().Any(a =>
                         a.GetValue(p, null) != null && a.GetValue(p, null).ToString().ToUpper().Contains(Search_text.ToUpper()))).ToObservableCollection();

         //Data = new ListCollectionView(filter)
       }
       else
       {
         //Filter works, but very slowly with memory leak
         Data.Filter = delegate (object item)
         {    
            if (!(item is DataModel data))
            {
               return false;
            }
    
             return (Selected_Name != null ? data.NAME == "Lucy" : true);
    
        };
    }
    catch (Exception ex)
    {
      //...
    }
}

Upvotes: 0

Views: 162

Answers (0)

Related Questions