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. (Last updated on : Nov 16th 2018).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

persist filter settings on data refresh

Thread ID:

Created:

Updated:

Platform:

Replies:

118538 Mar 17,2015 10:50 PM UTC Mar 24,2015 05:16 AM UTC WPF 6
loading
Tags: GridDataControl
Henry Harris
Asked On March 17, 2015 10:50 PM UTC

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?

Massi
Replied On 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!

Henry Harris
Replied On 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());
                }   
            }
            
            
        }

Massi
Replied On 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);
                    }
                }
            }
        }

Jai Ganesh S [Syncfusion]
Replied On 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


Henry Harris
Replied On March 23, 2015 05:39 PM UTC

Got it to work.  Thanks so much for your help

Jai Ganesh S [Syncfusion]
Replied On 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


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

;