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

[Solved] Grabbing Grouped, Filtered Records from a GridView

4 Answers 169 Views
GridView
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Luis Rizo
Top achievements
Rank 1
Luis Rizo asked on 22 Oct 2009, 03:15 PM
My issue is basically the same as the one described in this thread:
http://www.telerik.com/community/forums/silverlight/gridview/data-from-a-grouped-filtered-gridview.aspx

Basically I've got a GridView that is filtered and grouped at runtime and I need to be able to grab records at the lowest level (my DataClass records).

The example project posted in that thread shows how to grab the low-level records assuming that there's 1 group, but in my situation we may have any number of groups. Is there really no way to grab the final lowest level records from the filtered gridview results?


4 Answers, 1 is accepted

Sort by
0
Rossen Hristov
Telerik team
answered on 23 Oct 2009, 08:45 AM
Hello Luis Rizo,

With our latest releases we obsoleted records and now you can work directly with you business objects through the Items collection of the grid. Here is how to iterate over a grouped and filtered grid:

<UserControl x:Class="TicketID_251792_CustomReorderColumns.MainPage"
    xmlns:telerik="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.GridView"
    xmlns:Data="clr-namespace:Telerik.Windows.Data;assembly=Telerik.Windows.Data"
    mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
      <Button Content="Iterate"
              Click="OnIterate"
              Margin="10"
              HorizontalAlignment="Left"
              FontSize="20"/>
        <telerik:RadGridView Name="playersGrid"
                             Grid.Row="1"
                             AutoGenerateColumns="False"
                             ColumnsWidthMode="Auto"
                             AutoExpandGroups="True">
          <telerik:RadGridView.GroupDescriptors>
            <!--Group by Country and Position-->
            <Data:GroupDescriptor Member="Country"/>
            <Data:GroupDescriptor Member="Position"/>
          </telerik:RadGridView.GroupDescriptors>
          <telerik:RadGridView.FilterDescriptors>
            <!--Netherlands and England-->
            <Data:FilterDescriptor Member="Country"
                                   Operator="Contains"
                                   Value="land"/>
          </telerik:RadGridView.FilterDescriptors>
            <telerik:RadGridView.Columns>
                <telerik:GridViewDataColumn Header="Name"
                                            DataMemberBinding="{Binding Name}">
                </telerik:GridViewDataColumn>
                <telerik:GridViewDataColumn Header="Number"
                                            DataMemberBinding="{Binding Number}">
                </telerik:GridViewDataColumn>
                <telerik:GridViewDataColumn Header="Position"
                                            DataMemberBinding="{Binding Position}">
                </telerik:GridViewDataColumn>
                <telerik:GridViewDataColumn Header="Country"
                                            DataMemberBinding="{Binding Country}">
                </telerik:GridViewDataColumn>
            </telerik:RadGridView.Columns>
        </telerik:RadGridView>
    </Grid>
</UserControl>

And the code-behind:

using System.Collections;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using Telerik.Windows.Controls;
using Telerik.Windows.Data;
 
namespace TicketID_251792_CustomReorderColumns
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
 
            this.playersGrid.ItemsSource = Club.GetPlayers();
        }
 
        private void OnIterate(object sender, RoutedEventArgs e)
        {
            Debug.WriteLine("Current items are:");
            Debug.WriteLine("------------------");
            IterateRecursive(this.playersGrid.Items);
        }
 
        private static void IterateRecursive(IEnumerable items)
        {
            foreach (var item in items)
            {
                IGroup group = item as IGroup;
                if (null != group)
                {
                    IterateRecursive(group.Items);
                }
                else
                {
                    Player player = (Player)item;
                    Debug.WriteLine(player);
                }
            }
        }
    }
}

I have attached a sample project that demonstrates this. Note that you will need either out Latest Internal Build or the BETA2 build. You can get them from your Client.NET.

Let me know if you have any further questions.

All the best,
Ross
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Luis Rizo
Top achievements
Rank 1
answered on 23 Oct 2009, 02:02 PM
Thank you for your help.

I implemented a recursive function similar to yours and it seems to be working, with one exception. What I'm trying to do is grab the filtered records (no matter what groupings are applied), so I'm handling the GridView's Filtered event and calling my recursive function at that point to grab the Filtered record set. However, the first time that a filter is applied, my .Items property still has all the records (the record count matches the original binded source count).

After the first Filter is applied, this is no longer the case, and everything works great. But the first time that a Filter is applied, even though the DataGrid reflects the filter, the Items property (during the "Filtered" event), does not reflect any filtering.

I don't know if it makes a difference, but I'm using Asynchronous for the DataLoadingMode.

Any hints on this?
0
Luis Rizo
Top achievements
Rank 1
answered on 23 Oct 2009, 02:54 PM
Hello again.

Not sure if what I described in my previous post is a bug or a "feature", but I coded up a workaround in case someone else is having the same issue. Below is a codesnippet of my event handler for the GridView's Filtered event, along with my recursive function definition to grab all the data.

EDIT:
The code I added filtered my results correctly, but it caused undesired visual behavior in the Gridview, so I had to remove it. The situation previously described is still occurring and I'm still open to ideas.
0
Rossen Hristov
Telerik team
answered on 26 Oct 2009, 10:23 AM
Hi Luis Rizo,

With your help we have identified a bug. The bug caused the Filtered to be thrown before the actual filtering occurs in some cases. That is why when you get the Items.Count it is still the unfiltered one. We have already fixed the bug and the Filtered event should work correctly for the upcoming Q3 Release which is only a couple of weeks away.

We would like to apologize for the inconvenience. Your account has been updated with 1000 Telerik Points for helping us identify this issue.

Best wishes,
Ross
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Tags
GridView
Asked by
Luis Rizo
Top achievements
Rank 1
Answers by
Rossen Hristov
Telerik team
Luis Rizo
Top achievements
Rank 1
Share this question
or