How to get the count of specific rows visible items in a WPF datagrid?

Boom Ba 166 Reputation points
2022-07-18T14:22:39.033+00:00

Say I have a database table with columns ID, Name, Age, Salary etc.

I have a datagrid where I have set the itemsource as EmpDB where EmpDB is an ICollectionView and then in the MainViewModel I set it as

			EmpDB = new ListCollectionView(_allEmps)  
			{  
				Filter = o => ((Employees)o).Name == SelectedItemFromComboBox  
			};  

where class Employees represents the database table (the model), _allEmps is an ObservableCollection<Employees> and SelectedItemFromComboBox is a string property for the selected combo box item.

Now when I select an item from the combo box then the datagrid gets filled with EmpDB items and I can get the row count of the items currently visible in the datagrid and display it on a TextBlock using something like

<TextBlock Text="{Binding EmpDB.Count} ... />  

But I also want to display say the row count of how many of the Employees that are currently shown in the datagrid whose Age is > 50, the first thing that came in my mind is to add a property to the ViewModel.

So I added a property inside my MainViewModel class and binded it to a textblock

		private int _empcount;  
  
		public int EmpCount  
		{  
			get  
			{  
				if (_empcount == 0) _empcount = _allEmps.Where(x => x.Age > 50).Count();  
  
				return _empcount;  
  
			}  
  
			set  
			{  
				if (_empcount != value)  
				{  
					_empcount = value;  
					OnPropertyChanged("EmpCount");  
				}  
			}  
		}  

The problem is when I launch the app the textblock text bound to EmpCount should be 0 (the datagrid is empty as I have not selected any item from the combo box yet) but I get the count of all Employees whose Age > 50 and that count does not change when I select a different or any combo box item.

What am I missing and where ? Can anyone show me how to fix it ?

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

Accepted answer
  1. Hui Liu-MSFT 48,676 Reputation points Microsoft External Staff
    2022-07-28T10:24:03.36+00:00

    MainViewModel :

     public class MainViewModel : ViewModelBase  
        {  
            public ObservableCollection<string> ComboItems { get; set; }  
    
            public ICollectionView EmpDB { get; set; }  
            public ObservableCollection<Employees> _allEmps { get; set; }  
    
    
            private string _SelectedItemFromComboBox;  
            public string SelectedItemFromComboBox  
            {  
                get { return _SelectedItemFromComboBox; }  
                set  
                {  
                    SetValue(ref _SelectedItemFromComboBox, value);  
    
                    EmpDB.Refresh();  
                    OnPropertyChanged("EmpCount");  
                }  
            }  
            public int EmpCount  
            {  
                get  
                {  
                    return EmpDB.OfType<Employees>().ToList().Where(x => x.Age > 50).Count();  
    
                }  
    
            }  
            public MainViewModel()  
            {  
                DataAccess d = new DataAccess();  
    
                _allEmps = d.Emp;  
    
                ComboItems = new ObservableCollection<string>(_allEmps.Select(b => b.Name).Distinct().OrderBy(b => b).ToList());  
    
                EmpDB = new ListCollectionView(_allEmps)  
    
                {  
                    Filter = o => ((Employees)o).Name == SelectedItemFromComboBox  
                };  
            }  
     }  
    

    The result:

    225736-image.png


    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

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Rajanikant Hawaldar 91 Reputation points
    2022-07-18T15:06:54.35+00:00

    As I understand you need to display datagrid row count on text block.

    <TextBlock
    Content="{Binding ElementName=DataGrid, Path=Items.Count}" />


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.