Loss of Sort When a Filter is Present

3 posts, 1 answers
  1. Corey
    Corey avatar
    26 posts
    Member since:
    Nov 2012

    Posted 10 Sep 2013 Link to this post

    I am trying to have a RadGridView save its sorting descriptors and pass them on to a new RadGridView.  I need this functionality, because when a user refreshes a view, the previous RadGridView is destroyed and a new one is created.  This new view may have different data (depending on the user's actions), but we wish to have the old RadGridView's filters and sorts carried over to the new RadGridView.

    So far, I have managed to have the filters persist across the two RadGridViews, and I have managed to have the sorts persist when there are no filters on the grid.  However, if there is both a filter on the grid and a sort (not even in the same column), only the filter persists.  The sort is ignored.  Please see the pictures for more details.

    My code that saves the filter and sort descriptors is as follows:
    List<FilterSetting> filterSettings = new List<FilterSetting>();
    foreach (IColumnFilterDescriptor columnFilter in RadGridView.FilterDescriptors)
    {
      FilterSetting columnFilterSetting = new FilterSetting();
      columnFilterSetting.ColumnUniqueName = columnFilter.Column.UniqueName;
      columnFilterSetting.SelectedDistinctValues.AddRange(columnFilter.DistinctFilter.DistinctValues);
     
      if (columnFilter.FieldFilter.Filter1.IsActive)
      {
        columnFilterSetting.Filter1 = new FilterDescriptorProxy();
        columnFilterSetting.Filter1.Operator = columnFilter.FieldFilter.Filter1.Operator;
        columnFilterSetting.Filter1.Value = columnFilter.FieldFilter.Filter1.Value;
        columnFilterSetting.Filter1.IsCaseSensitive =
            columnFilter.FieldFilter.Filter1.IsCaseSensitive;
      }
     
      columnFilterSetting.FieldFilterLogicalOperator = columnFilter.FieldFilter.LogicalOperator;
     
      if (columnFilter.FieldFilter.Filter2.IsActive)
      {
        columnFilterSetting.Filter2 = new FilterDescriptorProxy();
        columnFilterSetting.Filter2.Operator = columnFilter.FieldFilter.Filter2.Operator;
        columnFilterSetting.Filter2.Value = columnFilter.FieldFilter.Filter2.Value;
        columnFilterSetting.Filter2.IsCaseSensitive =
            columnFilter.FieldFilter.Filter2.IsCaseSensitive;
      }
     
      filterSettings.Add(columnFilterSetting);
    }
     
    List<SortSetting> sortSettings = new List<SortSetting>();
    foreach (ColumnSortDescriptor columnSort in RadGridView.SortDescriptors)
    {
      SortSetting columnSortSetting = new SortSetting();
      columnSortSetting.ColumnUniqueName = columnSort.Column.UniqueName;
      columnSortSetting.Direction = columnSort.SortDirection;
     
      sortSettings.Add(columnSortSetting);
    }
     
    RunTimeData = new RunTimeData()
    {
      SelectedTags = this.SelectedTags,
      GridFilters = filterSettings,
      GridSorts = sortSettings
    };

    And here's the code that applies these saved filters and sorts to the new RadGridView inside its Loaded event:
    if (RunTimeData != null && this._dataTable.Rows.Count > 0)
    {
      this.SelectedTags = RunTimeData.SelectedTags;
     
      RadGridView.FilterDescriptors.SuspendNotifications();
      RadGridView.SortDescriptors.SuspendNotifications();
     
      foreach (var c in RadGridView.Columns)
      {
        if (c.ColumnFilterDescriptor.IsActive)
          c.ClearFilters();
      }
     
      foreach (FilterSetting setting in RunTimeData.GridFilters)
      {
        SynColumn column = (SynColumn)RadGridView.Columns[setting.ColumnUniqueName];
         
        if (column != null)
        {
          Telerik.Windows.Controls.GridView.IColumnFilterDescriptor columnFilter =
                column.ColumnFilterDescriptor;
     
          foreach (object distinctValue in setting.SelectedDistinctValues)
            columnFilter.DistinctFilter.AddDistinctValue(distinctValue);
     
          if (setting.Filter1 != null)
          {
            columnFilter.FieldFilter.Filter1.Operator = setting.Filter1.Operator;
            columnFilter.FieldFilter.Filter1.Value = setting.Filter1.Value;
            columnFilter.FieldFilter.Filter1.IsCaseSensitive = setting.Filter1.IsCaseSensitive;
          }
     
          columnFilter.FieldFilter.LogicalOperator = setting.FieldFilterLogicalOperator;
     
          if (setting.Filter2 != null)
          {
            columnFilter.FieldFilter.Filter2.Operator = setting.Filter2.Operator;
            columnFilter.FieldFilter.Filter2.Value = setting.Filter2.Value;
            columnFilter.FieldFilter.Filter2.IsCaseSensitive = setting.Filter2.IsCaseSensitive;
          }
        }
      }
     
      foreach (SortSetting setting in RunTimeData.GridSorts)
      {
        ColumnSortDescriptor columnSort = new ColumnSortDescriptor();
        columnSort.SortDirection = setting.Direction;
        columnSort.Column = RadGridView.Columns[setting.ColumnUniqueName];
     
        RadGridView.SortDescriptors.Add(columnSort);
      }
     
      RadGridView.FilterDescriptors.ResumeNotifications();
      RadGridView.SortDescriptors.ResumeNotifications();
     
      RunTimeData = null;
    }


    Please let me know what I may be missing or doing wrong.  It would be nice to have both filters and sorts persist when creating a new RadGridView instead of one or the other as it is now.  Thanks.
  2. Answer
    Rossen Hristov
    Admin
    Rossen Hristov avatar
    2478 posts

    Posted 11 Sep 2013 Link to this post

    Hello,

    Please try restoring one of them first and then the other -- not simultaneously. In other words:

    RadGridView.FilterDescriptors.SuspendNotifications();
    ...
    RadGridView.FilterDescriptors.ResumeNotifications();

    RadGridView.SortDescriptors.SuspendNotifications();
    ...
    RadGridView.SortDescriptors.ResumeNotifications();

    If this does not help, please send us a sample project and we will debug it.

    Regards,
    Rossen Hristov
    Telerik
    TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for SILVERLIGHT.
    Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
    Sign up for Free application insights >>
  3. DevCraft banner
  4. Corey
    Corey avatar
    26 posts
    Member since:
    Nov 2012

    Posted 16 Sep 2013 Link to this post

    Thank you so much!

    Sorry for the late reply.  The email was collected by my spam folder for some reason.  This fixed it.  Glad to see a simple solution.

    Thanks again!
Back to Top