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

ObservableCollection<T> - filter not applying to grid

7 Answers 283 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Lars
Top achievements
Rank 1
Lars asked on 27 Jun 2008, 11:22 AM
Hi Telerik,

It seems to me the RadDataGrid is not updating it self when applying a filter to its datasource of type ObservableCollection<T>:

I've tried the same code - just using the standard ListBox - and that works as expected.
The filter is applied and the listbox is updated to only show the items that apply to the filter - but that doesn't work for the RadDataGrid:

Here's a little code example:

//_availableOnBoardSalesProducts is of the type ObservableCollection<Products>  
gridFood.ItemsSource = _availableOnBoardSalesProducts;  
myListBox.ItemsSource = _availableOnBoardSalesProducts;  
 
//Add an item to the ObservableCollection works fine in the RadDataGrid  
        private void Button_Click(object sender, RoutedEventArgs e)  
        {  
            _availableOnBoardSalesProducts.Add(new OnboardSalesService.Products() { Price = 10m, Description = "MyDesc", Category = "MyCat" });  
        }  
 
//But applying a filter only works fine for the standard ListBox - not for the RadDataGrid  
        private void Button_Click_1(object sender, RoutedEventArgs e)  
        {  
            ICollectionView view = CollectionViewSource.GetDefaultView(gridFood.ItemsSource);  
            if (view.Filter == null)  
            {  
                view.Filter = delegate(object item)  
                {  
                    bool result = ((OnboardSalesService.Products)item).Price == 10m;  
                    return result;  
                };  
            }  
            else 
            {  
                view.Filter = null;  
            }  
 
        } 

Thats a bug right?

I've also noticed that you have an Telerik.Windows.Controls.Core.RadObservableCollection<T> available.
Whats that for?

7 Answers, 1 is accepted

Sort by
0
Atanas
Telerik team
answered on 01 Jul 2008, 11:31 AM
Hello Lars,

Currently our RadGridView does not listen for datasource filter change events. The RadGridView works with our own data engine, which handles the GroupDescriptions of ICollectionView, in future we have the intention to make the same implementation for SortDescriptions and the Filter property of ICollectionView.

The RadObservableCollection<T> is implemented for internal use only, it inherits Collection<T> and implements INotifyCollectionChanged and INotifyPropertyChanged, so if you need a collection which raises property and collection changed events you can use it.

Sincerely yours,
Atanas
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
Lars Berg
Top achievements
Rank 1
answered on 01 Jul 2008, 01:27 PM
OK, I hope you consider making it soon - since I need it in my current project :)

I think it will be of use to many others.
1) ObservableCollection<T> is the preferred notifiable collection in WPF (?)

2) And in VS 2008 when adding a new Service Reference - you can configure that collections returned will be of the type ObservableCollection<T> (instead of array)
So in VS 2008 its really easy to get up in running with a solution that contains a WCF service project that returns ObservableCollection<T> to a WPF project.

Anyway do you have a good workaround for this?
It seems that the Radgrid listens to add/remove of items on the ObservableCollection - so maybe I should simulate a filter by adding and removing items from the observablecollection
0
Hristo Deshev
Telerik team
answered on 02 Jul 2008, 12:58 PM
Hello Lars Berg,

We have been investigating the filtering issue with regards to collection views and here is what we found:
  • binding to a collection and then modifying its default collection view works with "standard" controls. That works because those controls internally work with the default collection view and the CollectionViewSource.GetDefaultCollectionView() method will cache a collection's collection view and that allows for modification of the observable collection's collection view.
  • RadGridView implements its own data logic that will do grouping/sorting and does not rely on the WPF collection view functionality.We made that design choice because we did not get the desired performance characteristics from the collection view-based data operations.
It is likely that we will not be supporting the scenario of binding to an observable collection and then modifying its default collection view to define filtering. There is a good workaround though -- getting the default collection view yourself and passing it as an ItemsSource value for the RadGridView control. In other words, your code should look something like:

ICollectionView view = CollectionViewSource.GetDefaultView(_availableOnBoardSalesProducts); 
gridFood.ItemsSource = view;

...


ICollectionView view = CollectionViewSource.GetDefaultView(_availableOnBoardSalesProducts); 
if (view.Filter == null) 

    view.Filter = delegate(object item) 
    { 
        bool result = ((OnboardSalesService.Products)item).Price == 10m; 
        return result; 
    }; 

else

    view.Filter = null; 


Do you think that would be OK as a requirement to your client code? By the way, the current Beta 2 release has a bug that will prevent the grid from picking up the filtered items from a collection view -- we have that fixed and the final release (scheduled due July 22) won't suffer from this problem.

To address your ObservableCollection<T> vs. RadObservableCollection<T> concerns, I'd like to mention that we do not require you to use RadObservableCollection in any way. The class is used by RadGridView-related collections and it includes several improvements that are really grid-specific. RadGridView fully supports binding to a regular ObservableCollection-based collections and should be fully WCF-compliant.

Kind regards,
Hristo Deshev
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
Lars Berg
Top achievements
Rank 1
answered on 02 Jul 2008, 02:18 PM
Hi, thanks for looking into it.

I haven't tried your suggesion - since as I understand you say that it doesn't work in beta 2.

But if your workaround works it should be ok for my solution.

The main difference as I read it, is that I should set ItemSource to an ICollectionView instead of directly to the ObservableCollection. I'll try that with the final version.
0
Hristo Deshev
Telerik team
answered on 02 Jul 2008, 02:31 PM
Hello Lars Berg,

Yep, we do not respect the collection view filter in Beta 2. By the way, I might be able to send you a build with that problem fixed, earlier than the release if it is crucial for your solution -- just open up a separate support ticket to request that.

All the best,
Hristo Deshev
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
Jay
Top achievements
Rank 1
answered on 24 Mar 2011, 09:16 PM
has this stance changed since this posting?

let me know.

thanks!
0
Rossen Hristov
Telerik team
answered on 25 Mar 2011, 09:54 AM
Hi Jay,

A lot of things have changed for the last three years.

RadGridView works with something called FilterDescriptors. When you add a FilterDescriptor to RadGridView, you will filter it. You can learn more about the current state of filtering under the Filtering category of the online documentation. 

Here are some other useful links:

Online Demos 

 

How-to Articles

 

Blog Posts

I hope this helps.

Regards,

Ross
the Telerik team
Tags
General Discussions
Asked by
Lars
Top achievements
Rank 1
Answers by
Atanas
Telerik team
Lars Berg
Top achievements
Rank 1
Hristo Deshev
Telerik team
Jay
Top achievements
Rank 1
Rossen Hristov
Telerik team
Share this question
or