pivotGridControl Filter Event

Good day, I would like to be able to run some code any time the "Ok" button is clicked in the filter selection dialog. I cannot seem to find an event that is triggered at this point. I tried the FilterActionCompleted event but that is not called. Is there anyway to tie some code in automatically when the filters are changed? in particular I am pulling certain pieces of data from the grid by simply selecting the desired range. I would like for that same range to be re-selected and the new data pulled when the grid is filtered.
Also, is there a way to programmatically choose the filter fields and the specific items to be filtered for?
Thanks!

3 Replies

SR Sabaridass Ramamoorthy Syncfusion Team June 22, 2018 12:15 PM UTC

Hi Travis, 
 
Please find our response below. 
 
 Is there anyway to tie some code in automatically when the filters are changed? 
As per the current behavior of PivotGrid control, we have provided the “FilterActionCompleted” event handler method only for RowPivotsOnly mode.  
 
If you would like you implement your own logic after applying filters, then you can use the “Filters_CollectionChanged” event handler method in your application. CollectionChanged event will be invoked once the filters are applied into the filters collection of PivotGrid control. 
 
Please refer to the below code example. 
#Form.cs 
 
private void InitializePivotGrid() 
        { 
            pivotGridControl1.ItemSource = ProductSalesData.GetSalesData(); 
 
            pivotGridControl1.PivotRows.Add(new PivotItem { FieldMappingName = "Product", TotalHeader = "Total" }); 
            pivotGridControl1.PivotRows.Add(new PivotItem { FieldMappingName = "Year", TotalHeader = "Total" }); 
 
            pivotGridControl1.PivotColumns.Add(new PivotItem { FieldMappingName = "Country", TotalHeader = "Total" }); 
            pivotGridControl1.PivotColumns.Add(new PivotItem { FieldMappingName = "State", TotalHeader = "Total" }); 
 
            pivotGridControl1.PivotCalculations.Add(new PivotComputationInfo { FieldName = "Amount", Format = "$ #,##0.00" }); 
            pivotGridControl1.PivotCalculations.Add(new PivotComputationInfo { FieldName = "Quantity", Format = "#,##0" }); 
 
            //enables to display the group bar 
            pivotGridControl1.ShowGroupBar = true; 
 
            pivotGridControl1.AllowFiltering = true; 
            pivotGridControl1.AllowSorting = true; 
 
            //tab key navigation set as false to move the next control 
            pivotGridControl1.TableControl.WantTabKey = false; 
 
            this.pivotGridControl1.TableModel.Options.AllowSelection = Syncfusion.Windows.Forms.Grid.GridSelectionFlags.Any; 
            this.pivotGridControl1.TableModel.SelectionChanged += TableModel_SelectionChanged; 
            this.pivotGridControl1.Filters.CollectionChanged += Filters_CollectionChanged; 
            this.pivotGridControl1.TableControl.DataRefreshed += TableControl_DataRefreshed; 
        } 
 
void Filters_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) 
        { 
            // You can write your code here... 
            persistSelection = true; 
        } 
 
 
 I would like for that same range to be re-selected and the new data pulled when the grid is filtered. 
By default, maintaining the grid selections for every operation is not the proper behavior. Since some of the selected cells will be removed from grid control when applying filters. So that only we have cleared the grid selections when we are applying filters. 
 
But you can maintain the grid selection after applying filters if you want. To achieve this, you should use the “SelectionChanged” and “DataRefreshed” event handlers in your application. 
 
Please refer to the below code example. 
#Form.cs 
 
private void InitializePivotGrid() 
        { 
            pivotGridControl1.ItemSource = ProductSalesData.GetSalesData(); 
 
            pivotGridControl1.PivotRows.Add(new PivotItem { FieldMappingName = "Product", TotalHeader = "Total" }); 
            pivotGridControl1.PivotRows.Add(new PivotItem { FieldMappingName = "Year", TotalHeader = "Total" }); 
 
            pivotGridControl1.PivotColumns.Add(new PivotItem { FieldMappingName = "Country", TotalHeader = "Total" }); 
            pivotGridControl1.PivotColumns.Add(new PivotItem { FieldMappingName = "State", TotalHeader = "Total" }); 
 
            pivotGridControl1.PivotCalculations.Add(new PivotComputationInfo { FieldName = "Amount", Format = "$ #,##0.00" }); 
            pivotGridControl1.PivotCalculations.Add(new PivotComputationInfo { FieldName = "Quantity", Format = "#,##0" }); 
 
            //enables to display the group bar 
            pivotGridControl1.ShowGroupBar = true; 
 
            pivotGridControl1.AllowFiltering = true; 
            pivotGridControl1.AllowSorting = true; 
 
            //tab key navigation set as false to move the next control 
            pivotGridControl1.TableControl.WantTabKey = false; 
 
            this.pivotGridControl1.TableModel.Options.AllowSelection = Syncfusion.Windows.Forms.Grid.GridSelectionFlags.Any; 
            this.pivotGridControl1.TableModel.SelectionChanged += TableModel_SelectionChanged; 
            this.pivotGridControl1.Filters.CollectionChanged += Filters_CollectionChanged; 
            this.pivotGridControl1.TableControl.DataRefreshed += TableControl_DataRefreshed; 
        } 
 
        void TableControl_DataRefreshed(object sender, Syncfusion.Windows.Forms.PivotAnalysis.DataRefreshedEventArgs e) 
        { 
            if (persistSelection && activeRanges != null) 
            { 
                this.pivotGridControl1.TableModel.SelectedRanges.Clear(); 
                for (int i = 0; i < activeRanges.Count; i++) 
                { 
                    this.pivotGridControl1.TableModel.SelectedRanges.Add(activeRanges[i]); 
                } 
            } 
        } 
 
        void TableModel_SelectionChanged(object sender, Syncfusion.Windows.Forms.Grid.GridSelectionChangedEventArgs e) 
        { 
            for (int i = 0; i < this.pivotGridControl1.TableModel.SelectedRanges.Count; i++) 
            { 
                if (!activeRanges.Contains(this.pivotGridControl1.TableModel.SelectedRanges[i])) 
                    activeRanges.Add(this.pivotGridControl1.TableModel.SelectedRanges[i]); 
            } 
        } 
 
 
 
Also, is there a way to programmatically choose the filter fields and the specific items to be filtered for? 
Yes. You can apply filters based on your own filter expression and it can be achieved by adding the “FilterExpression” into the Filters collection. 
 
Please refer to the code example below. 
#Form.cs 
 
  private void button2_Click(object sender, EventArgs e) 
        { 
            if (this.pivotGridControl1.Filters.Any(x => x.Name == "State")) 
                this.pivotGridControl1.Filters.Remove(this.pivotGridControl1.Filters.Where(x => x.Name == "State").FirstOrDefault()); 
            this.pivotGridControl1.Filters.Add(new FilterExpression() { Name = "State", DimensionName = "State", Expression = "State = Alberta || State = Ontario" }); 
        } 
 
        private void button1_Click(object sender, EventArgs e) 
        { 
            if (this.pivotGridControl1.Filters.Any(x => x.Name == "Product")) 
                this.pivotGridControl1.Filters.Remove(this.pivotGridControl1.Filters.Where(x => x.Name == "Product").FirstOrDefault()); 
            this.pivotGridControl1.Filters.Add(new FilterExpression() { Name = "Product", DimensionName = "Product", Expression = "Product = Bike" }); 
        } 
 
 
 
We have created a simple sample to achieve your requirement and please find the sample from below location. 
 
If any of our solution does not meet your actual requirement, please provided the detailed information about your requirement. So that we can provided the solution at the earliest. 
 
Regards, 
Sabaridass R 
 



TC Travis Chambers June 23, 2018 04:49 PM UTC

Thank you for the update! Unfortunately the filter collectiom changed event doesnt really help me though because at that point we only know what filter fields are present not what items are selected for filtering in the filter selection dialogue box. Is there any way to capture the moment when the "Ok" button is clicked in that filter dialogue? Unfortunately i can not use row pivots only for my purposes.


SR Sabaridass Ramamoorthy Syncfusion Team June 25, 2018 12:21 PM UTC

  
As we said earlier, we do not have any event handler method to be invoked when the “OK” button was clicked except “Filters_CollectionChanged” event handler. If you want to get the list of selected items from filter dialog box when the “OK” button is pressed, then you can also get them from “Filters_CollectionChanged” event handler. Please refer to the below code example which is helps you to get the selected items from filter dialog box.  
  
#Form1.cs  
  
void Filters_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)  
        {  
            // You can write your code here...  
            persistSelection = true;  
            var filterExpression = e.NewItems[0] as FilterExpression;  
            if(filterExpression != null && filterExpression.Tag is FilterItemsCollection)  
            {  
                List<FilterItemElement> listOfSelectedItems = (filterExpression.Tag asFilterItemsCollection).Where(x => x.IsSelected == true).ToList();  
            }  
        }  
  
  
Please find the working sample from the below location.  
  
If you are still unable to achieve your requirements using “Filters_CollectionChanged” event handler, then kindly share all your exact requirements. So that we can provide an improvement/feature based on your requirements.  
  
Regards,  
Sabaridass R  
 


Loader.
Up arrow icon