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

Using the CollectionViewSource

5 Answers 432 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Sean
Top achievements
Rank 1
Sean asked on 31 Aug 2011, 12:33 PM
Hello I recently tried to use a CollectionViewSource as the items source for the RadGridView in WPF. I wanted to do so for the ammount of filtering that I needed. Coming from a Windows development background that was "the way" to do it. I am new to my company and I am not trying to fall behind. Is there an example of how to do this? In C# all we have to do to apply a filter is add a filter event handler, but when I would do this the RadGrid would "hiccup." I ended up having to use a dataview object before pulling my hair out. I refuse to write for loops with the invention of generics in the .Net framework. Any and all help is appreciated.

Sean

5 Answers, 1 is accepted

Sort by
0
Rossen Hristov
Telerik team
answered on 31 Aug 2011, 12:52 PM
Hi Sean,

You can filter RadGridView view both through the UI (the little funnels) and programmatically.

Please, take a look at my blog post for more information. You should also examine all sub-topics under the Filtering topic in our documentation.

Let me know if you have any other questions.

Kind regards,
Ross
the Telerik team

Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

0
Sean
Top achievements
Rank 1
answered on 31 Aug 2011, 02:14 PM
Thanks for the quick response. I have the current understanding that the most efficient way to filter an object in WPF is to use the CollectionViewSource. That way I can add button filters above the grid, etc. Doing something like this:

private void AddAFilter(object sender, RoutedEventArgs e)
   {
       cvs.Filter += new FilterEventHandler(FilterOutA);
   }
    
   private void RemoveAFilter(object sender, RoutedEventArgs e)
   {
       cvs.Filter -= new FilterEventHandler(FilterOutA);
   }

that way on the Checked and Unchecked methods this makes filtering a DataGridView in WPF very easy. I tried to set the DataContext with CollectionViewSource, I also tried to add it as a Resource in my xaml and then finally trying to use the ItemsSource in the RadGridView. I could not apply one filter that way. I got an error from the UI when it tried to update. We deal with large amounts of data and it is the best approach for efficiency on the CPU. I read the post and it showed how to add them using around about way. Is it necessary to do this with the Telerik controls? Is there a place that has what the common translations from a regular .Net object's properties into what is needed with your guys' controls? Thanks again for the quick response. I was impressed.


Sean
0
Sean
Top achievements
Rank 1
answered on 01 Sep 2011, 06:18 AM
I didn't offend anyone did I? I never got a response back. I saw how in your blog that I can filter data using your controls, but it seems a bit convoluted. Is there a way to use the CollectionViewSource? It seems the cleanest way. I know I can bind a custom object to a grid, so why can't I bind that custom object to a radgridview? Thanks again. I am asking in earnest. Any and all help is appreciated.


Sean
0
Accepted
Rossen Hristov
Telerik team
answered on 01 Sep 2011, 09:19 AM
Hi Sean,

As far as I understood, you want to filter the data before you "feed" it to RadGridView (or any other ItemsControls as a matter of fact). If I am correct, then you should disable RadGridView's built-in filtering. 

Here is what RadGridView does when you "feed" it with a simple IEnumerable. It takes this IEnumerable and wraps it in an internal IQueryable. The when the user performs filtering (and sorting and grouping as a matter of fact), our data engine does the following. It read the information stored in the FilterDescriptors of RadGridView and based on that information it creates a lambda expression, compiles it and then dynamically invokes it over the source collection. In pseudo-code this might look like this:

sourceCollection.Where(customer => customer.Name == "John"). 

The result from this LINQ operation is then displayed in the grid. But that is if you leave RadGridView do the job for you. If you want, you can filter all the data before it even reaches RadGridView (or any ItemsControls such as a ListBox) but then RadGridView will not be a part of the filtering.

I hope that this explains things a little bit more.

All the best,
Ross
the Telerik team

Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

0
Sean
Top achievements
Rank 1
answered on 01 Sep 2011, 12:47 PM
Thank you so much. It's an odd thing adjusting to someone else's controls when you are used to writing them yourself! Just mine don't look as nice! I really appreciate the help. I voted and will keet voting for you guys.


Gratefully,

Sean
Tags
GridView
Asked by
Sean
Top achievements
Rank 1
Answers by
Rossen Hristov
Telerik team
Sean
Top achievements
Rank 1
Share this question
or