Skip to main content Skip to footer

Save and Load Enhanced Filters in Spread

The new features of Spread version 7 includes Enhanced filtering which is similar to the filtering provided in Excel. You can now select multiple values from the filter dropdownlist to perform filtering based on multiple values. Along with this Spread provides you with three different types of filters : Number Filters, Date Filters and Text Filters that provides you with various filtering options, for example, "Equals to", "Contains" and many more. There is another option to perform custom filtering where in you can apply these filters with a combination of "And" and "Or" operators. You may refer to the following for a detailed elaboration of these features: http://helpcentral.componentone.com/NetHelp/SpreadNet7/WF/spwin-enhanced-filter-use.html Sometimes, the user needs an option to save the filter state applied to one of the Spread sheets, so that the same filtering state can be set on another Spread or same Spread at a later stage. For default filters i.e. where the filtering is performed based on a single value, you can simply use the GetColumnFilterBy method that returns the filter value to save the filter state. But, when we talk about the enhanced filters, achieving the same requires some more effort. This blog describes how you can save and load the enhanced filters. Moving on the same track we use the GetColumnFilterBy method to get the filter string. This method returns two strings in case of enhanced filtering i.e. either "Multi Values Filter" or "Custom Filter" depending on the filtering performed. When method returns "Multi Values filter" string, we can create an instance of the MultiValuesFilterItem class to get the current filter strings. The filter strings returned are simple strings that can be stored in a string array. And, when this method returns "Custom Filter", we create an instance of the CustomFilterItem class to get the current filtering condition. This filtering condition has three different values including the FirstCondition, SecondCondition and the value of the "And" or "Or" operator. You would need to save the values of all these three parameters to save the custom filter state. Here is the code that depicts how we can save both the MultiValuesFilter and CustomFilter:


private void button1_Click(object sender, EventArgs e)  
{  
  FarPoint.Win.Spread.IRowFilter rf = fpSpread1.ActiveSheet.RowFilter;  
  foreach (FilterColumnDefinition fcd in rf.ColumnDefinitions)  
  {  
    FilterItemCollection filters = fcd.Filters;  
    IFilterItem iFilterItem = null;  
    string filterString = fpSpread1.Sheets[0].RowFilter.GetColumnFilterBy(0);  
    for (int i = 0, count = filters.Count; i < count; i++)  
    {  
      if (filters[ i ].DisplayName == filterString)  
       {  
         iFilterItem = filters[ i ];  
         break;  
       }  
    }  
    if (iFilterItem is FarPoint.Win.Spread.MultiValuesFilterItem)  
    {  
      multival = true;  
      customfil = false;  
      MultiValuesFilterItem mvFilterItem = iFilterItem as MultiValuesFilterItem;  
      if (mvFilterItem != null)  
      {  
        filterby = new string[mvFilterItem.FilterItems.Count];  
        int i = 0;  
        foreach (FarPoint.Win.Spread.FilterItemValue fi in mvFilterItem.FilterItems)  
        {  
          filterby[ i ] = fi.Value.ToString();  
          i++;  
        }  
      }  
    }  
    else if (iFilterItem is FarPoint.Win.Spread.CustomFilterItem)  
    {  
      customfil = true;  
      multival = false;  
      CustomFilterItem cfFilterItem = iFilterItem as CustomFilterItem;  
      filterby = new string[3];  
      if (cfFilterItem != null)  
      {  
        fc1 = cfFilterItem.FirstCondition;  
        fc2 = cfFilterItem.SecondCondition;  
        and = cfFilterItem.And;  
      }  
    }  
  }  
 MessageBox.Show("Filter state saved");  
}  

Once the filters are saved, it is quite easy to reset them on the new sheet. All you need to do is to define new instances for MultiValuesFilterItem class and CustomFilterItem class. You would be using the filtering values saved above to set the values in these filters. After defining the filters, make sure that the "SheetView" property of the filter is assigned the sheetview object on which the filtering is to be performed. If this property is not set, no data shows up in the sheet. Here is the code snippet that implements the suggested approach:


private void button2_Click(object sender, EventArgs e)  
{  
  fpSpread2.ActiveSheet.ClearRowFilter();  
  fpSpread2.ActiveSheet.SetColumnAllowFilter(0, true);  
  if (multival)  
  {  
    FilterItemValueCollection fc = new FilterItemValueCollection();  
    for (int r = 0; r < filterby.Length; r++)  
    {  
      fc.Add(new FarPoint.Win.Spread.FilterItemValue(filterby[r]));  
    }  
    MultiValuesFilterItem filter = new MultiValuesFilterItem(fc);  
    filter.SheetView = fpSpread2.ActiveSheet;  
    fpSpread2.ActiveSheet.RowFilter.ColumnDefinitions[0].Filters.Add(filter);  
    fpSpread2.ActiveSheet.AutoFilterColumn(0, filter.DisplayName, 0);  
    MessageBox.Show("Saved filter state applied to second spread");  
  }  
  else if (customfil)  
  {  
    CustomFilterItem cff = new CustomFilterItem(fc1, fc2, and);  
    cff.SheetView = fpSpread2.ActiveSheet;  
    fpSpread2.ActiveSheet.RowFilter.ColumnDefinitions[0].Filters.Add(cff);  
    fpSpread2.ActiveSheet.AutoFilterColumn(0, cff.DisplayName, 0);  
    MessageBox.Show("Saved filter state applied to second spread");  
  }  
}  

Lets have a look how the final output looks like: Output Refer to the attached samples for complete implementation. Download C# Sample Download VB Sample

MESCIUS inc.

comments powered by Disqus