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

Get Filtered GridView Items

28 Answers 224 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Nick Wood
Top achievements
Rank 1
Nick Wood asked on 02 Mar 2010, 10:56 PM
Hi

I posted this request in a related post, but i think I need to start a new thread.

I am using the aggregate example to bind a chart to the gridview.itemsource but I need to bind to the filtered source. The grouping all works fine, but if the user filteres the collection, I need to access the filtered results rather than all the results.

I have been pointed to the gridview.items collection, but as soon as you group the grid, the base items in the collection are put within a group and you can no longer iterate through the top level, you need to iterate through each group.

All I need is just to be able to return a list of filtered items from the grid.

please help

Nick

28 Answers, 1 is accepted

Sort by
0
Accepted
Rossen Hristov
Telerik team
answered on 03 Mar 2010, 03:07 PM
Hello Nick Wood,

You are correct. When the grid is grouped, you can use a very simple recursion to iterate over all of the leafs, i.e. normal data items.

I have prepared a small class with three extension methods that encapsulate this recursion. You can add this extension methods to your project and your RadGridView will get a new property called FlatItems that you can use whenever you want to get the flat list of leaf items. In fact you can always use it.

Here are the three extensions methods:

using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections;
using System.Collections.Generic;
using Telerik.Windows.Data;
using System.Linq;
using Telerik.Windows.Controls.GridView;
 
namespace TicketID_286445_GetFilteredItemsFromGroupedGrid
{
    public static class GroupExtensions
    {
        public static IEnumerable Leaves(this IEnumerable<IGroup> groups)
        {
            foreach (var group in groups)
            {
                foreach (var item in group.Leaves())
                {
                    yield return item;
                }
            }
        }
 
        public static IEnumerable Leaves(this IGroup group)
        {
            if (group == null)
            {
                return Enumerable.Empty<object>();
            }
 
            if (!group.HasSubgroups)
            {
                return group.Items;
            }
 
            return group.Subgroups.Leaves();
        }
 
        public static IEnumerable FlatItems(this GridViewDataControl grid)
        {
            if (grid.IsGrouping)
            {
                return grid.Items.Cast<IGroup>().Leaves();
            }
 
            return grid.Items;
        }
    }
}

I have prepared a small sample project that uses the FlatItems property to list all currently visible players. If the grid is not grouped FlatItems will simply return Items, so you can always use this property.

You can find the sample project attached. I hope this helps. Let is know if there are any problems.

Regards,
Ross
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Nick Wood
Top achievements
Rank 1
answered on 04 Mar 2010, 04:40 AM
Hi Ross

This is perfect!.

Can I ask how exactly did you extend the gridview with the GroupExtensions class?

Thankyou very much for all your help
0
Rossen Hristov
Telerik team
answered on 04 Mar 2010, 08:41 AM
Hi Nick Wood,

I have extended the grid by creating an extension method called FlatItems.

What exactly would you like to know? I will try to explain it, just let me know which part is unclear.

I have also posted both the source code for this and a sample project.

Kind regards,
Ross
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Nick Wood
Top achievements
Rank 1
answered on 04 Mar 2010, 08:53 AM
Hi Ross

It is just unclear how you used the GroupExtensions class to extend the GridView.

The source code in the project that you attached has a gridview that is already extended with the GroupExtensions class. It is possible to see how you performed this extension?

I would just use the binary for the gridview that you attached, but it is a trial version and we have the paid dev version.


0
Rossen Hristov
Telerik team
answered on 04 Mar 2010, 09:15 AM
Hello Nick Wood,

You have to add those extensions methods to your project. They are not part of the RadGridView assembly and they will never be. They are just extension methods.

Just add a new class to the project where your grid is used, and paste the three methods that I have created. The name of the class is not important at all. It only has to be static.

If you want to learn more about extension methods and how to create and use them, please take a look at this article.

I hope this helps. Let me know if there is something unclear.

Greetings,
Ross
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Nick Wood
Top achievements
Rank 1
answered on 04 Mar 2010, 09:44 AM
Hi Ross

Now I get what you are doing. Thankyou very much for your help. telerik support is fantastic! You really do go above and beyond.

Nick
0
Nick Wood
Top achievements
Rank 1
answered on 23 Apr 2010, 06:09 AM
Hi, Running this code in the new telerik Silverlight 4 controls released and now I get the below error:

public static IEnumerable Leaves(this IEnumerable<IGroup> groups)
        {
            foreach (var group in groups)
            {
                foreach (var item in group.Leaves())
                {
                    yield return item;
                }
            }
        }

on the line: foreach (var group in groups). Error:
Unable to cast object of type 'PMSDash.Interface.DALClassExtenstions.ProductProcessSuperViewVw_InteractivePress' to type 'Telerik.Windows.Data.IGroup'.
0
Rossen Hristov
Telerik team
answered on 23 Apr 2010, 08:51 AM
Hi Nick Wood,

We have made a breaking change. You will no longer need to use those extension methods. Now, even if RadGridView is grouped, its Items property will return the normal "leaf" items. In case you want the groups, it has a Groups property. So, if you want to get all items that are currently in the grid, simply use its Items property.

Let us know if problems arise.

Sincerely yours,
Ross
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Julian
Top achievements
Rank 1
answered on 21 Jun 2011, 08:37 AM
Hi,

Is it applicable to WPF apps too ?

thx.
Julian
0
Rossen Hristov
Telerik team
answered on 21 Jun 2011, 08:44 AM
Hello Julian,

We share the same code-base between SL and WPF, so it is.

Regards,
Ross
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Julian
Top achievements
Rank 1
answered on 21 Jun 2011, 09:29 AM
Hi,

Well, I have a few additional questions if you will :)

1. How to determine if the filter has been applied (some events, properties/flags) to gridview.
2. I tried to use the following strategy to meet my needs (determine when filter has been applied and collection changed):

this.gridView.Items.CollectionChanged+=(s,e) =>
{
   int count = this.gridView.Items.Count;
}

The issue is that CollectionChangedEvent  raising few times during collection is filtering.
At the beginning the amount of items in the grid were 2000 items
1. First time it raises with 2000 times
2. Second time 500 items
3. Third time 2 items (filtered data that I need).

Any suggestions ? :)
Thanks,

Julian
 



0
Rossen Hristov
Telerik team
answered on 21 Jun 2011, 10:10 AM
Hi Julian,

A filter is applied on a column when the FilterDescriptors collection of RadGridView contains the respective ColumnFilterDescriptor.

Please read my blog post in order to understand how filtering actually works.

Alternatively, you can detect that RadGridView was filtered when the Filtered event occurs. Have in mind though that this event will occur when filters are being cleared as well as when filters are being added.

If you try to explain what exactly are you trying to achieve I might be able to give you better advice.

Greetings,
Ross
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Julian
Top achievements
Rank 1
answered on 21 Jun 2011, 10:29 AM
Ok :)

Well the thing is that I'm trying to send a value to my custom filter via DP.
The custom filter has a Dependency Propeprty:

public static readonly  DependencyPropeprty GetFromDataProperty = DependencyProperty
.Register(
"GetfromDate",
typeof(object),
typeof (CustomFilterBase<TimeFilterEnum>, // this is a base type for a few filters in my app.
new PropertyMetadata(false, ItemSorceChanged));

in the View where data grid placed I've added a binding in codebehind (this is another story - the filter control is located in another visual tree, that is why I need to implement my binding in codebehind):

//create new Binding
var bindingTimeStampFromDateFilter = new Binding()
{
  Source = this.radGridView,
 Path = new PropertyPath(""), // I don't know what kind of value I have to put here :)))) still investigating.
Mode = BindingMode.TwoWay
};

//set binding for timefilter
this.timeFilter.SetBinding(TimeFilterContrl.GetFromDataProperty , bindingTimeStampFromDateFilter );

Well as you see I'd like to update my filter with filtered data when items were filtered :)

Can you suggest how can I do that ? :)

Julian

0
Rossen Hristov
Telerik team
answered on 21 Jun 2011, 11:29 AM
Hi Julian,

Unfortunately I could not quite understand what are you doing exactly and I have absolutely no idea about your custom filter and what it is.

In case you are asking where you can get the filtered data from the answer is RadGridView.Items.

As for your original question which was "How to determine if the filter has been applied (some events, properties/flags) to gridview.", did you at least try what I have written in mu previous post?

Kind regards,
Ross
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Julian
Top achievements
Rank 1
answered on 21 Jun 2011, 11:36 AM
I tried to use FilteredEvent and it doesn't works for me. I don't know why.
0
Rossen Hristov
Telerik team
answered on 21 Jun 2011, 11:42 AM
Hi Julian,

The RadGridView.Filtered event is always raised when the grid is either being filtered or being de-filtered.

The RadGridView.FilterDescriptors collection will raise the CollectionChanged event when RadGridView is being either filtered or de-filtered.

If you attach and listen for one of those two events you can easily detect that the grid is being filtered or being de-filtered.

It is as simple as that. Attach to either of these events and show a MessageBox in the event handler in order to see that they work perfectly well.

I really hope this helps.

Best wishes,
Ross
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Julian
Top achievements
Rank 1
answered on 21 Jun 2011, 11:53 AM
OK,
Thank a lot for helping.

regards,
Julian
0
Julian
Top achievements
Rank 1
answered on 21 Jun 2011, 01:59 PM
Hi,

an Event Filtered doesn't works in case of custom filter.

Can u please provide me with an example where custom filter works with mentioned event.

Thank you,
Julian.
0
Rossen Hristov
Telerik team
answered on 21 Jun 2011, 02:01 PM
Hi Julian,

Are you the one that is developing the custom filter? 

Kind regards,
Ross
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Julian
Top achievements
Rank 1
answered on 21 Jun 2011, 02:08 PM
Sorry, but I'm not really understand your question. What do you mean ?
Julian
0
Rossen Hristov
Telerik team
answered on 21 Jun 2011, 02:17 PM
Hello Julian,

Let us try to sort things our since we are not getting anywhere. I would like you to answer a series of questions that will help me understand what exactly are you doing on you end. Thanks in advance for your answers.

What do you mean by "custom filter"?
Is this a custom filtering control like the one I have demonstrated in my blog post?
In case it is -- are you the author, i.e. the person that is developing it?
In case you are, are you the one that adds and removes ColumnFilterDescriptors to the FilterDescriptors collection of RadGridView?

In case "custom filter" is not a custom filtering controls, then what exactly is it?

Why, I quote "an Event Filtered doesn't works in case of custom filter."? Maybe you can share some details regarding this statement.

I just cannot guess what you are doing on your end and you are not helping me either, so please try to describe what you are doing and why isn't it working.

All the best,

Ross
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Julian
Top achievements
Rank 1
answered on 21 Jun 2011, 02:50 PM
Hi,

Ok let me be more clear.

- Yes it is a custom control that is similar to your control (actually you can find the same custom filter in project with examples - TimeFilter).

I've added this custom filter control into the xaml:
                          .....
                <telerik:GridViewDataColumn Header="Time"
                                            DataMemberBinding="{Binding TimeStamp}"
                                            TextTrimming="CharacterEllipsis"
                                            TextWrapping="NoWrap"
                                            IsResizable="False"
                                            IsFilterable="True">
                               <telerik:GridViewDataColumn.FilteringControl>         
<
CustomFilters:TimeFilterControl x:Name="timeFilter"/>

                                                              </telerik:GridViewDataColumn.FilteringControl>
        

        

</telerik:GridViewDataColumn>
                .....

in code behind I'm trying to subscribe to mentioned event and perform some actions:

this.radGridView.Filtered += (s, e) =>
            {
                var bindingTimeStampFromDataFilter = new Binding()
                {
                    Source = this.radGridView,
                    Path = new PropertyPath("Items"),
                    Mode = BindingMode.Default
                };

                this.timeFilter.SetBinding(TimeFilterControl.GetFromDataProperty, bindingTimeStampFromDataFilter);
            };

when I'm trying to filter the radgridview it works ok, but an event is not raising.

Julian

0
Rossen Hristov
Telerik team
answered on 21 Jun 2011, 03:15 PM
Hello Julian,

So everything is normal then.

The Filtered Event is raised only by our stock control. Raising the Filtered event is our way to tell the developer (you) that the end user has performed filtering through the UI.

When you replace our stock filtering control with your own custom filtering control you take control over things -- you know when filtering occurs and there is no need for an event to be raised. Filtering occurs when you filter the grid by adding a ColumnFilterDescriptor to its FilterDescriptors collection (or removing it from there). You are the one that does this so you know when it is happening.

You don't need an event to understand that something that you are doing has happened because you are the one that is doing it.

Here is my suggestion. To solve all of your problems.

Attach to the following two events:

RadGridView.FilterDescriptors.CollectionChanged
RadGridView.FilterDescriptors.ItemChanged

When your custom filtering control adds a ColumnFilterDescriptor to RadGridView.FilterDescriptors collection it will raise the CollectionChanged event with Action of Add, because you are adding a ColumnFilterDescitptor to this collection.

When your custom filtering control removes a ColumnFilterDescriptor from RadGridView.FilterDescriptors collection it will raise the CollectionChanged event with Action of Remove, because you are removing a ColumnFilterDescitptor to this collection.

When a certain change is made on an existing ColumnFilterDescriptor that is contained in RadGridView.FilterDescriptors collection, it will raise its ItemChanged event indicating that something on this descriptor has been changed.

You can attach and listen for these events, even though I don't see the point, since you are the one that is causing them in first place.

It is like serving a tennis ball from one side of the court, then running before the ball reaches the other side of the court and receiving it. You are in fact playing with yourself. Causing something and then wanting to listen for it. Does my metaphor make sense to you?

Listening to something which your very own code causes seems totally illogical to me, but you can do it in any way you want to.

So, please go ahead and attach to the events I mentioned above (RadGridView.FilterDescriptors.CollectionChanged and RadGridView.FilterDescriptors.ItemChanged).

Place breakpoints in their event handlers and see when they are hit. Also, examine the event arguments each time they are being hit. This will help you realize that filtering is nothing more that adding, modifying or removing one thing (ColumnFilterDescriptor) to a collection of other things (RadGridView.FilterDescriptors).

Examining the event handlers with your debugger will help you understand how simple filtering actually is.

I really hope this makes sense.

Let me know if you have absolutely no idea what I am talking about and I will try to explain it once again.

All the best,

Ross
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Julian
Top achievements
Rank 1
answered on 21 Jun 2011, 03:37 PM
Hi,

I'm really appreciated for such description.
But I really disagree that I have to use your approach - in my case, it is without any sense to use it.
FilterDescriptor.ItemChanged or/and FilterDescriptor.CollectionChanged events.

In case of using mentioned events they will notify you that filters are added or removed, but they will not solve the main puzzle - notify the end user that items in the radfridview were filtered and provide with current-filtered items.
In case of using Filtered event it will raise when all filters applied and afterward I'll be able to check the current-filtered items in the grid.

You sad: Listening to something which your very own code causes seems totally illogical to me, but you can do it in any way you want to.

What is illogical for you ? I'm listening to RadGridView and not to my custom filters. In case of my custom filters I just update them with appropriative data when RadGridView will be filtered. More precisely saying: I have custom filter control which has two fields: From Date and To Date. When you open the filter you'll able to see From Date with the value (20.01.2011) that was get from the RadGridView.Items[0], that was previously filtered.

In conclusion, I'm totally disagree with you that it is illogical. Or maybe you didn't get the question clear.

Julian

0
Rossen Hristov
Telerik team
answered on 21 Jun 2011, 07:46 PM
Hi Julian,

You are totally free to use any approach you find correct and applicable. The API is there, you can use it in any way that will suit your particular needs. In case you don't agree with me, I guess that it will be up to you to implement your custom logic. I have done my best to help you, but it seems that we can't really connect.

Good luck!

Regards,
Ross
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Julian
Top achievements
Rank 1
answered on 22 Jun 2011, 08:23 AM
Good Morning Ross,

Seems to be that we are really misunderstood each other few hours ago.

I'd like to let you know, that I figured out how to solve my issue. And it was definitely done with CollectionChangedEvent.

Thanks for your time and patience :)

Julian
0
Rossen Hristov
Telerik team
answered on 22 Jun 2011, 08:26 AM
Hello Julian,

In the way that I understood your requirements, I still think that this is not the correct way to do it, but hey, maybe I did not understand your requirements correctly and we were talking about different things all this time. The good news is that you have found the solution -- this is the important thing here.

Best wishes,
Ross
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Julian
Top achievements
Rank 1
answered on 22 Jun 2011, 08:46 AM
Hi Ross,

There is one little thing that I forgot to mention yesterday :) I have 4 filters in the grid (4 custom filters). When app is started 3 of 4 filters are activated and data is filtered. The last one (not activated yet) is "Time Filter" control and it contains two Text Boxes, namely: From Date and To Date. When data has been filtered by those three filters I have to update the value of my 4th time filter, namely: From Date value with the value that I get from already filtered data. And now I can filter data once again with 4th filter that is Time Filter.

Is it clear now ? :)
Julian
Tags
GridView
Asked by
Nick Wood
Top achievements
Rank 1
Answers by
Rossen Hristov
Telerik team
Nick Wood
Top achievements
Rank 1
Julian
Top achievements
Rank 1
Share this question
or