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!

Developer technologies Windows Presentation Foundation
Developer technologies XAML
Developer technologies C#
{count} votes

1 answer

Sort by: Most helpful
  1. Hui Liu-MSFT 48,676 Reputation points Microsoft External Staff
    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.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.