We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

persist filter settings on data refresh

Is there anyway to keep the data filters when we change the underlying data?  For instance, if we filter based on records created in the last day and then refresh the data, the filter will go away as soon as we change the data source.  Is there a way around this?

6 Replies

MA Massi March 18, 2015 12:07 PM UTC

Hi Henry,
i was ranking my brain for the two last days finding a solution for this same issue,
i finally came out with a solution in two step
  1. handle the Grid.Model.FilterChanged event and loop trough all visibles columns and save in a dictionary each column with the associated filter  if one in applied, then save this dictionary in the Tag property of the Grid.
  2. handle the Grid.ItemsSourceChanged event and  check for the tag property if not null then loop again through visibles columns a set the value from the dictionnary
hope will be help!


HH Henry Harris March 18, 2015 04:15 PM UTC

could you provide a little more info.  Here is how I thought it would be implemented but col.Filters is read only so how can i set the filters?


  private Dictionary<Syncfusion.Windows.Controls.Grid.GridDataVisibleColumn, IEnumerable<FilterPredicate>> FilterDict = new Dictionary<Syncfusion.Windows.Controls.Grid.GridDataVisibleColumn, IEnumerable<FilterPredicate>>();

  void AccountGrid_ItemsSourceChanged(object sender, Syncfusion.Windows.ComponentModel.SyncfusionRoutedEventArgs args)
        {
            foreach (GridDataVisibleColumn col in this.AccountGrid.Model.GetVisibleColumns())
            {
                 
                    if (FilterDict.ContainsKey(col))
                         col.Filters=FilterDict[col];
                    
                 
            }
        }

        void Model_FilterChanged(object sender, Syncfusion.Windows.Controls.Grid.GridFilterEventArgs e)
        {
            foreach (GridDataVisibleColumn col in this.AccountGrid.Model.GetVisibleColumns())
            {
                if (col.Filters != null)
                {
                    if (FilterDict.ContainsKey(col))
                        FilterDict[col] = col.Filters.AsEnumerable();
                    else
                        FilterDict.Add(col, col.Filters.AsEnumerable());
                }   
            }
            
            
        }


MA Massi replied to Henry Harris March 19, 2015 02:22 PM UTC

could you provide a little more info.  Here is how I thought it would be implemented but col.Filters is read only so how can i set the filters?


  private Dictionary<Syncfusion.Windows.Controls.Grid.GridDataVisibleColumn, IEnumerable<FilterPredicate>> FilterDict = new Dictionary<Syncfusion.Windows.Controls.Grid.GridDataVisibleColumn, IEnumerable<FilterPredicate>>();

  void AccountGrid_ItemsSourceChanged(object sender, Syncfusion.Windows.ComponentModel.SyncfusionRoutedEventArgs args)
        {
            foreach (GridDataVisibleColumn col in this.AccountGrid.Model.GetVisibleColumns())
            {
                 
                    if (FilterDict.ContainsKey(col))
                         col.Filters=FilterDict[col];
                    
                 
            }
        }

        void Model_FilterChanged(object sender, Syncfusion.Windows.Controls.Grid.GridFilterEventArgs e)
        {
            foreach (GridDataVisibleColumn col in this.AccountGrid.Model.GetVisibleColumns())
            {
                if (col.Filters != null)
                {
                    if (FilterDict.ContainsKey(col))
                        FilterDict[col] = col.Filters.AsEnumerable();
                    else
                        FilterDict.Add(col, col.Filters.AsEnumerable());
                }   
            }
            
            
        }

Hi Henry,
this is my code 

  void Model_FilterChanged(object sender, Syncfusion.Windows.Controls.Grid.GridFilterEventArgs e)
        {
            //FilterDict is the dictionary that hold column mapping name an associated filters
            var FilterDict = new Dictionary<string, ObservableCollection<Syncfusion.Windows.Data.FilterPredicate>>();
            
            // step1 copy applied fiters to FilterDict
            foreach (var vc in productGrid.VisibleColumns)
            {
                if (vc.Filters != null && vc.Filters.Count> 0)
                {
                    var prediates = new ObservableCollection<Syncfusion.Windows.Data.FilterPredicate>();
                    foreach (var fpr in vc.Filters)
                    {
                        prediates.Add(new Syncfusion.Windows.Data.FilterPredicate
                        {
                            FilterBehavior = fpr.FilterBehavior,
                            FilterType = fpr.FilterType,
                            FilterValue = fpr.FilterValue,
                            IsCaseSensitive = fpr.IsCaseSensitive,
                            PredicateType = fpr.PredicateType
                        });
                    }

                    FilterDict.Add(vc.MappingName, prediates);

                }
            }
                    
            // step2 save FilterDic in GridDataControl Tag property


            if (FilterDict.Count > 0)
            {
                productGrid.Tag = FilterDict;
            }
            else 
            {
                //' important! set productGrid.Tag to nothing if grid columns filter are clear
                productGrid.Tag = null;
            
            }
                    
        }



 void productGrid_ItemsSourceChanged(object sender, Syncfusion.Windows.ComponentModel.SyncfusionRoutedEventArgs args)
        {

            if (productGrid.ItemsSource == null)
            {
                return;
            }
            
            // Get the saved filter from Tag properties of the 
            var FilterDict = productGrid.Tag as Dictionary<string, ObservableCollection<Syncfusion.Windows.Data.FilterPredicate>>;
            if (FilterDict == null)
            { return; }

            // Restore the save filters for each visible column

            foreach (var dictEntry in FilterDict)
            {
                if (dictEntry.Value.Count > 0)
                {
                    productGrid.VisibleColumns[dictEntry.Key].Filters.Clear();

                    foreach (var fpredicate in dictEntry.Value)
                    {
                        productGrid.VisibleColumns[dictEntry.Key].Filters.Add(fpredicate);
                    }
                }
            }
        }


JG Jai Ganesh S Syncfusion Team March 20, 2015 02:12 AM UTC

Hi Hentry,

 

Thank you for using Syncfusion Products.

 

We have analyzed your query.  You can achieve your requirement by using the code suggested by Massi for this forum  and we have prepared the sample based on that code  and please find the sample  under the following location,

 

Sample: http://www.syncfusion.com/downloads/support/directtrac/136494/GDCFilteringDemo943334933.zip

 

@Massi: We have appreciate for posting a response to this forum and thank you for contribution.

 

Thank you,

Jai Ganesh S



HH Henry Harris March 23, 2015 05:39 PM UTC

Got it to work.  Thanks so much for your help


JG Jai Ganesh S Syncfusion Team March 24, 2015 05:16 AM UTC

Hi Hentry,

Thank you for the update.

Please let us know if you have any queries.

Thank you,

Jai Ganesh S


Loader.
Live Chat Icon For mobile
Up arrow icon