This is a migrated thread and some comments may be shown as answers.

Filter and Sort RadVirtualGrid via code

4 Answers 171 Views
VirtualGrid
This is a migrated thread and some comments may be shown as answers.
Luis
Top achievements
Rank 1
Luis asked on 21 Nov 2017, 11:40 AM

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,

4 Answers, 1 is accepted

Sort by
0
Stefan
Telerik team
answered on 24 Nov 2017, 09:31 AM
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.
0
Luis
Top achievements
Rank 1
answered on 24 Nov 2017, 05:11 PM

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();               
    }
 
}
0
Luis
Top achievements
Rank 1
answered on 24 Nov 2017, 05:57 PM

Also,

How could update my rankGrid.InitialRowCount?

0
Stefan
Telerik team
answered on 29 Nov 2017, 12:57 PM
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.
Tags
VirtualGrid
Asked by
Luis
Top achievements
Rank 1
Answers by
Stefan
Telerik team
Luis
Top achievements
Rank 1
Share this question
or