Filter and Sort RadVirtualGrid via code

5 posts, 0 answers
  1. Luis
    Luis avatar
    11 posts
    Member since:
    Sep 2017

    Posted 21 Nov Link to this post

    Dear Sirs,

    I would like to know how to call the method FieldFilterEditorCreated, or apply a ColumnFilterDescriptor to a not filtered DataProvider, or any way to filter a column via code (preferably using ColumnFilterDescriptor).
    Also, I would like to do the same with sorting (using ColumnSortDescriptor or any possible way).

    I appreciate any help.
    Thanks,

  2. Stefan
    Admin
    Stefan avatar
    806 posts

    Posted 24 Nov Link to this post

    Hello Luis,

    Such mechanism is not exposed by the default implementation of the DataProvider of RadVirtualGrid. To achieve such filtering/sorting, you need to implement a custom DataProvider and override its ApplyFilterDescriptor/ApplySortDescriptor methods. Can you please give it a try?

    Hopefully, this helps.

    Regards,
    Stefan
    Progress Telerik
    Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which allow you to write beautiful native mobile apps using a single shared C# codebase.
  3. Luis
    Luis avatar
    11 posts
    Member since:
    Sep 2017

    Posted 24 Nov in reply to Stefan Link to this post

    Dear Stefan,

    Thank you for answering me.

     

    /* CustomDataProvider */
    public ColumnFilterDescriptor FilterDescriptor
            {
                get
                {
                    return filterDescriptor;
                }
     
                set
                {
                    filterDescriptor = value;
                }
            }
     
    public void ApplyFilter()
            {
                ApplyFilterDescriptor(filterDescriptor);
            }
     
            protected override void ApplyFilterDescriptor(ColumnFilterDescriptor descriptor)
            {
                if (filterDescriptor != descriptor)
                {
                    if (filterDescriptor == null)
                    {
                        filterDescriptor = descriptor;
                    }
                    else if (
                        (string.IsNullOrEmpty(descriptor.FieldFilter.Filter1.Value.ToString())
                        && !string.IsNullOrEmpty(filterDescriptor.FieldFilter.Filter1.Value.ToString()))
                        ||
                        ((string.IsNullOrEmpty(descriptor.FieldFilter.Filter2.Value.ToString()))
                        && !string.IsNullOrEmpty(filterDescriptor.FieldFilter.Filter2.Value.ToString()))
                            )
                    {
                        descriptor = filterDescriptor;
                    }
                    else if (
                       descriptor.DistinctFilter.ToString() == "Empty"
                       &&
                       filterDescriptor.DistinctFilter.ToString() != "Empty"
                       )
                    {
                        descriptor = filterDescriptor;
                    }
                }
     
                base.ApplyFilterDescriptor(descriptor);
            }
     
            protected override void RemoveAllColumnFilters()
            {
                base.RemoveAllColumnFilters();
            }
     
            protected override void RemoveColumnFilter(ColumnFilterDescriptor descriptor)
            {
                base.RemoveColumnFilter(descriptor);
            }

     

    As you can see, that's exactly what I did. Before substetuting my DataProvider for a new one I copy the FilterDescriptor property, then pass it to the new one and invoke the method ApplyFilter(). However, I still cannot handle multiple filters, like when there is more than one column filtered.

    The following code is in my view and is where do all the process explained before.

    private void RefreshGrid()
    {
        //verify if there is filter to be applied
        if (viewModel.DataProvider.FilterDescriptor != null)
        {
            filterDescriptor = viewModel.DataProvider.FilterDescriptor;
        }
     
        //...
            
        //at this point viewModel.Source is already up to date
        viewModel.DataProvider = new CustomDataProvider(viewModel.Source);
     
        //...
     
        rankGrid.DataProvider = viewModel.DataProvider;
     
        //set filter
        if (filterDescriptor != null)
        {
            viewModel.DataProvider.FilterDescriptor = filterDescriptor;
            viewModel.DataProvider.ApplyFilter();               
        }
     
    }
  4. Luis
    Luis avatar
    11 posts
    Member since:
    Sep 2017

    Posted 24 Nov in reply to Stefan Link to this post

    Also,

    How could update my rankGrid.InitialRowCount?

  5. Stefan
    Admin
    Stefan avatar
    806 posts

    Posted 29 Nov Link to this post

    Hi Luis,

    Internally, RadVirtualGrid utilizes QueryableCollectionView for managing its data operations. The DataProvider, on the other hand, exposes it through its Source property. Shortly said, you can access the FilterDescriptors collection of the QCV from the custom DataProvider and persist all FilterDescriptors through it. Then, you should be able to iterate over the persisted collection of FilterDescriptors and apply each one through the ApplyFilterDescriptor method. Please, give the approach a try and let me know how it goes.

    As to your second question, the implementation of the DataProvider of the control does not allows setting the InitialRowCount/InitialColumnCount properties. With one of the next internal builds, the DataProvider will expose a property holding the instance of RadVirtualGrid, through which you will be able to set the InitialRowCount and InitalColumnCount properties.

    Hopefully, I have managed to make things clearer. Do let me know in case further assistance is needed.

    Regards,
    Stefan
    Progress Telerik
    Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which allow you to write beautiful native mobile apps using a single shared C# codebase.
Back to Top