WPF Filtering DataGrid

Julian Hofmaninger 1 Reputation point
2021-12-25T23:23:58.447+00:00

Hi,
I have a small WPF application which gets some data (~300 rows) from an API and gets displayed in a DataGrid. Up to this point everything works well. Some Header-columns of the DataGrid have nested textboxes, which should serve as search-boxes. I bound their text to a property in my ViewModel and wrote a filter for the CollectionView. If I enter a filter-text everything is okay and I get the result I expect. The problem appears when I try to remove my filter-text again. When I want to remove the last character from the text-box the UI freezes for a few seconds. I think this comes from loading all the rows into the DataGrid again. Has anyone faced this problem as well?
Thanks for your help in advance!

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,676 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,278 questions
XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
766 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Hui Liu-MSFT 40,266 Reputation points Microsoft Vendor
    2021-12-27T06:50:07.053+00:00

    I use the following code for testing. If it meets your needs, you could refer to it.
    MainWindow.xaml:

     <StackPanel>  
        <TextBox Text="{Binding TextSearch,UpdateSourceTrigger=PropertyChanged}"/>  
        <DataGrid  Name="datagrid" VerticalAlignment="Top"  SelectedItem="{Binding SelectedEmployee}"   
                  ItemsSource="{Binding View}" AutoGenerateColumns="False" ScrollViewer.VerticalScrollBarVisibility="Visible"  >  
            <DataGrid.Columns>  
                <DataGridTextColumn Header="FirstName" Binding="{Binding FirstName}" Width="80"/>  
            </DataGrid.Columns>  
        </DataGrid>  
    </StackPanel>  
    

    MainWindow.xaml.cs:

    public partial class MainWindow : Window  
      {  
        public MainWindow()  
        {  
          InitializeComponent();  
          DataContext = new EmployeeListViewModel();  
        }  
      }  
      class EmployeeListViewModel : INotifyPropertyChanged  
      {  
        public event PropertyChangedEventHandler PropertyChanged;  
    
        public void OnPropertyChanged(String info)  
        {  
          if (PropertyChanged != null)  
          {  
            PropertyChanged(this, new PropertyChangedEventArgs(info));  
          }  
        }  
        public EmployeeListViewModel()  
        {  
          EmployeeList = new ObservableCollection<Employee>(GetEmployees());  
    
          this._view = new ListCollectionView(this.employeeList);  
          this._view.Filter=Filter;  
        }  
        private bool Filter(object item)  
        {  
          if(String.IsNullOrEmpty(TextSearch))  
            return true;  
          else  
            return((item as Employee).FirstName.IndexOf(TextSearch,StringComparison.OrdinalIgnoreCase) >=0);  
        }  
        private ObservableCollection<Employee> employeeList;  
        public ObservableCollection<Employee> EmployeeList  
        {  
          get { return employeeList; }  
          set  
          {  
            employeeList = value;  
            OnPropertyChanged("EmployeeList");  
          }  
        }  
        private ListCollectionView _view;  
        public ICollectionView View  
        {  
          get { return this._view; }  
        }  
    
        private string _TextSearch;  
        public string TextSearch  
        {  
          get { return _TextSearch; }  
          set  
          {  
            _TextSearch = value;  
            OnPropertyChanged("TextSearch");  
            View.Refresh();  
          }  
        }  
        private List<Employee> GetEmployees()  
        {  
          var mylist = new List<Employee>();  
          for(int i = 1; i < 500; i++)  
          {  
            mylist.Add(new Employee() { FirstName = $"nummer{i}" });  
          }  
          return mylist;  
        }  
      }  
      public class Employee  
      {  
        string firstname;  
    
        public string FirstName  
        {  
          get { return firstname; }  
          set { firstname = value; }  
        }  
      }  
    

    The result:
    160499-3.gif


    If the response is helpful, please click "Accept Answer" and upvote it.
     Note: Please follow the steps in our [documentation][5] to enable e-mail notifications if you want to receive the related email notification for this thread. 

    [5]: https://learn.microsoft.com/en-us/answers/articles/67444/email-notifications.html

    1 person found this answer helpful.