How do I bind to the record count after filter has changed

Hi,


Filtering ok on sfTreeGrid. But how do I bind to the record count after filtering?

I think I need to somehow hook up to the the treeGrid.View.RecordPropertyChanged?
Not sure how to do that as looks like the view is only created after setting the ItemSource in the ViewModel.


Thanks


7 Replies

DD Dhivyabharathi Dakshinamurthy Syncfusion Team May 10, 2024 06:06 PM UTC

Hi Lee Stevens,

We can get the records count after filtering was applied using “FilterChanged” event. Kindly refer the following code snippets.

  public MainWindow()

  {

      InitializeComponent();

      this.treeGrid.FilterChanged += TreeGrid_FilterChanged;

  }

 

  private void TreeGrid_FilterChanged(object? sender, Syncfusion.UI.Xaml.TreeGrid.Filtering.TreeGridFilterChangedEventArgs e)

  {

      var filteredRecords = this.treeGrid.View.Nodes.Count();

  }


Kindly provide the details, how you are tried to bind the record count after filtering. It will help us to provide the better solution.




LS Lee Stevens May 13, 2024 07:56 AM UTC

Thank you kindly for the reply.


I'm using WPF & MVC. I believe I won't have access to the treeGrid.View in my ViewModel from that event.

Also doesn't the view only get created after the datasource is set? Therfore I can't do anything in a behaviour either?






DD Dhivyabharathi Dakshinamurthy Syncfusion Team May 14, 2024 12:34 PM UTC

Hi Lee Stevens,

We suggested using the FilterChanged event, as the TreeGrid view will be accessible after loading the TreeGrid. However, you mentioned that you won't have access to treeGrid.View.

To assist you better, could you kindly provide the following details.

  1. How are you applying the filtering?
  2. Did you apply the filter during the initial loading of the TreeGrid.
  3. If not, what type of filter did you use? Specify the type of filter (e.g., CheckBoxFilter, RowFilter, AdvancedFilter).


LS Lee Stevens May 14, 2024 12:42 PM UTC

Hi,


Ok on the Xaml we apply a behaviour:


 <interactivity:Interaction.Behaviors>
     <behaviors:TreeGridFilterBehaviour />
 </interactivity:Interaction.Behaviors>


In the behaviour:


public class TreeGridFilterBehaviour : Behavior<SfTreeGrid>
{
    protected override void OnAttached()
    {
        base.OnAttached();
        this.AssociatedObject.Loaded += AssociatedObject_Loaded;
    }


    private void AssociatedObject_Loaded(object sender, System.Windows.RoutedEventArgs e)
    {
        CompanyOverviewViewModel viewModel = this.AssociatedObject.DataContext as CompanyOverviewViewModel;
        if (viewModel != null)
        {
            viewModel.filterChanged = OnFilterChanged;


THE VIEW IS NOT AVAILABLE HERE AS IT'S NOT CREATED UNTIL THE DATA SOURCE IS SET

            //var treeGrid = this.AssociatedObject as SfTreeGrid;
            //treeGrid.View.RecordPropertyChanged += viewModel.OnFilterChanged;
        }
    }


    private void OnFilterChanged()
    {
        var treeGrid = this.AssociatedObject as SfTreeGrid;


        var viewModel = this.AssociatedObject.DataContext as CompanyOverviewViewModel;
        if (treeGrid.View != null)
        {
            treeGrid.View.Filter = viewModel.FilterRecords;
            treeGrid.View.RefreshFilter();
        }
    }





DD Dhivyabharathi Dakshinamurthy Syncfusion Team May 15, 2024 05:58 PM UTC

Hi Lee Stevens,

We're able to access the treegrid.View and hooked the FilterChanged event in the Behavior.  If this doesn't meet your requirements or if we've misunderstood your scenario, please provide details about your viewModel.

Please refer to the following code snippets.

public class TreeGridFilterBehaviour : Behavior<SfTreeGrid>

 {

     protected override void OnAttached()

     {

         base.OnAttached();

         AssociatedObject.Loaded += AssociatedObject_Loaded;

         AssociatedObject.FilterChanged += AssociatedObject_FilterChanged;

     }

 

     private void AssociatedObject_FilterChanged(object? sender, Syncfusion.UI.Xaml.TreeGrid.Filtering.TreeGridFilterChangedEventArgs e)

     {

         var filteredRecords = AssociatedObject.View.Nodes.Count();

     }

 

     private void AssociatedObject_Loaded(object sender, System.Windows.RoutedEventArgs e)

     {

         var treeGrid = AssociatedObject as SfTreeGrid;

 

         var viewModel = AssociatedObject.DataContext as ViewModel1;

         if (treeGrid.View != null)

         {

             treeGrid.View.Filter = viewModel.FilerNodes;

             treeGrid.View.RefreshFilter();

         }

     }

 }  

 


We have attached the sample for your reference. Please have a look at this.


Attachment: SfTreeGrid_Behavior_fde49d87.zip


LS Lee Stevens May 21, 2024 01:19 PM UTC

Thank you for the excellent example.

Unfortuantley the event is not firing for us. Setup exactly as you have it.

I wonder if it's something to do with .net we are currently on 4.8 (slowly upgrading to .net 6!)

We might have to wait till that process is done.



DD Dhivyabharathi Dakshinamurthy Syncfusion Team May 22, 2024 01:42 PM UTC

Hi Lee Stevens,

This issue might not be related to specific behaviors with .NET 6.0 or Framework 4.8. Kindly check our implementation and compare it with your application; there may be a chance that something was missed. Please review and update us with your concerns.


Loader.
Up arrow icon