Transferring Filters from One RadGridView to Another

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

    Posted 09 May 2013 Link to this post

    I have a RadGridView that resembles the one below.  The grid allows for pop-up filtering on each column.  We allow the user to modify rows within the grid and once they are done we rebuild the entire page (which involves destroying the previous grid and building a new one).  Currently, we transfer any filters and selected row information from the previous grid to the new grid via a custom variable that grabs the RadGridView's FilterDescriptors.

    However, when we programmatically add these filters to the newly build grid during the Loaded event of the RadGridView (a la RadGridView.FilterDescriptors.Add(filter);), the new grid correctly filters on the designated columns in the right way, but it does so exclusively.  Using the example grid below, if I were to filter the grid on Route ID = 003 and then modify that row (triggering the building of a new grid on the page), the new grid would only display the 003 row (even though the Items attribute contains the other two rows) with no way of accessing the other rows and no filter on the grid even acknowledged.  Furthermore, if I try to filter on Route ID again, the only option for me to filter on is 003.  I've included some pictures to better explain my problem.

    What I'm hoping to accomplish is to allow the building of the new grid as it currently does, but to transfer the filters as they were from the previous grid.  I don't want the new grid to be locked into a hard filter on said filters.  Is this possible?


    TripSet ID          Date          Route ID
    010                     4/9/13         001
    010                     4/9/13         002
    010                     4/9/13         003
  2. Pavel Pavlov
    Admin
    Pavel Pavlov avatar
    2039 posts

    Posted 10 May 2013 Link to this post

    Hi,

    May you paste the code that takes care of storing/restoring the filter descriptors. Also it would be nice to know what is the source collection type.

    I will have a look and see what went wrong.

    Greetings,
    Pavel Pavlov
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

  3. DevCraft banner
  4. Corey
    Corey avatar
    26 posts
    Member since:
    Nov 2012

    Posted 10 May 2013 Link to this post

    Here is most of the code used to build the RadGridView (both the initial one and the new one).
    public RadGridView BuildGrid(DataTable dataTable, IEnumerable<WspViewRow> gridRows)
    {
       RadGridView radGrid = new RadGridView();
       
       radGrid.Loaded += new RoutedEventHandler(_radGrid_Loaded);
       
       BuildColumns(radGrid, dataTable);
       BuildRows(radGrid, dataTable, gridRows);
       
       radGrid.ItemsSource = dataTable;
       
       return radGrid;
    }
    BuildColumns creates custom GridViewDataColumns that we add to the RadGridView.  BuildRows binds the data accordingly and creates DataRows that we add to our DataTable ItemsSource.

    Here is the method that saves the previous grid's FilterDescriptions.  It saves some other things, but only the filters are worth noting at this point.
    protected override void SaveRunTimeData()
    {
       _runTimeData = new RunTimeData()
       {
          GridFilters = RadGridView.FilterDescriptors
       };
    }

    Lastly, here is our RadGridView.Loaded event handler that sets any filters from the previous grid.
    void _radGrid_Loaded(object sender, RoutedEventArgs e)
    {
       if (_runTimeData != null)
       {
          foreach (ColumnFilterDescriptor filter in _runTimeData.GridFilters)
             RadGridView.FilterDescriptors.Add(filter);
     
          _runTimeData = null;
       }
    }
  5. Answer
    Pavel Pavlov
    Admin
    Pavel Pavlov avatar
    2039 posts

    Posted 10 May 2013 Link to this post

    Hello,
    I have I have consulted with the team on your problem and here is the output :

    The column filter descriptors hold a reference to a column . When you recreate RadGridView brand new columns appear which are not the same instances with the old ones.

    The Filter descriptors still hold the old columns thus the erroneous behavior.

    The solution would be to create new filter descriptors based on the values in the old ones , rather than reusing them.

    You may see some working code in this online example. Check the code in the cs files for more info .

    All the best,
    Pavel Pavlov
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

  6. Corey
    Corey avatar
    26 posts
    Member since:
    Nov 2012

    Posted 10 May 2013 Link to this post

    Awesome.  Thanks Pavel.

    That fixed it and it's working great now.
Back to Top
DevCraft banner