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

[Solved] Iterate over visible rows (rows that are not filtered out)

3 Answers 303 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.
John
Top achievements
Rank 1
John asked on 07 Aug 2009, 08:32 PM
Greetings,

I am basically trying to iterate over all of the top-level rows in a RadGridView that are not filtered out. I have some code that seems to work using the Records property of the RadGridView, but apparently a couple of the related classes (Record/DataRecord) have been obsoleted (Q2.2009) and I can't seem to find substitutes. Here is a snippet of my code:

List<ControllerChannelType> controllerChannelTypesList = new List<ControllerChannelType>(); 
 
            foreach (Record record in this.GridView.Records) 
            { 
                DataRecord dataRecord = record as DataRecord; 
 
                System.Diagnostics.Debug.Assert(dataRecord != null); 
                if (dataRecord != null) 
                { 
                    ChannelView channelView = dataRecord.Data as ChannelView; 
 
                    System.Diagnostics.Debug.Assert(channelView != null); 
                    if(channelView != null) 
                    { 
                        controllerChannelTypesList.Add(channelView.Channel_ChannelType); 
                    } 
                } 
            } 

Ultimately, I am hoping to get a distinct list of values from a certain property from the list of bound business objects; however, it must be visible in the grid in order to be inserted into this list. Any thoughts on how I could achieve this using classes that are not obsoleted? Or maybe there is a better approach overall...

I should also mention that I am using Silverlight 2.

As always, thanks for your assistance.




3 Answers, 1 is accepted

Sort by
0
Rossen Hristov
Telerik team
answered on 12 Aug 2009, 11:46 AM
Hello John,

Make sure that you are using our latest internal build (which could be found in your Client.Net) and here is how it's done. My grid is bound to a list of Player business objects for the sake of the sample.

<UserControl x:Class="TicketID_233602_IterateOverUnfilteredDataItems.MainPage" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
    xmlns:telerik="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.GridView" 
    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"/> 
        <telerik:RadGridView Name="playersGrid" Grid.Row="1" AutoGenerateColumns="False" 
                ColumnsWidthMode="Auto"
            <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> 
 

using System.Collections.Generic; 
using System.Windows; 
using System.Windows.Controls; 
 
namespace TicketID_233602_IterateOverUnfilteredDataItems 
    public partial class MainPage : UserControl 
    { 
        public MainPage() 
        { 
            this.InitializeComponent(); 
            this.playersGrid.ItemsSource = Club.GetPlayers(); 
        } 
 
        private void OnIterate(object sender, RoutedEventArgs e) 
        { 
            var players = new List<Player>(); 
            foreach (object dataItem in this.playersGrid.Items) 
            { 
                players.Add((Player) dataItem); 
            } 
        } 
    } 

using System.ComponentModel; 
 
namespace TicketID_233602_IterateOverUnfilteredDataItems 
    /// <summary> 
    /// A football player. 
    /// </summary> 
    public class Player : INotifyPropertyChanged 
    { 
        public event PropertyChangedEventHandler PropertyChanged; 
 
        private string name; 
        private int number; 
        private Position position; 
        private string country; 
 
        public string Name 
        { 
            get { return this.name; } 
            set 
            { 
                if (value != this.name) 
                { 
                    this.name = value; 
                    this.OnPropertyChanged("Name"); 
                } 
            } 
        } 
 
        public int Number 
        { 
            get { return this.number; } 
            set 
            { 
                if (value != this.number) 
                { 
                    this.number = value; 
                    this.OnPropertyChanged("Number"); 
                } 
            } 
        } 
 
        public Position Position 
        { 
            get { return this.position; } 
            set 
            { 
                if (value != this.position) 
                { 
                    this.position = value; 
                    this.OnPropertyChanged("Position"); 
                } 
            } 
        } 
 
        public string Country 
        { 
            get { return this.country; } 
            set 
            { 
                if (value != this.country) 
                { 
                    this.country = value; 
                    this.OnPropertyChanged("Country"); 
                } 
            } 
        } 
 
        public Player(string name, int number, Position position, string country) 
        { 
            this.name = name; 
            this.number = number; 
            this.position = position; 
            this.country = country; 
        } 
 
        protected virtual void OnPropertyChanged(PropertyChangedEventArgs args) 
        { 
            PropertyChangedEventHandler handler = this.PropertyChanged; 
            if (handler != null
            { 
                handler(this, args); 
            } 
        } 
 
        private void OnPropertyChanged(string propertyName) 
        { 
            this.OnPropertyChanged(new PropertyChangedEventArgs(propertyName)); 
        } 
 
        public override string ToString() 
        { 
            return this.name; 
        } 
    } 
 

I am attaching the SL3 project that the above code snippets are from. You can easily copy paste the code in a SL2 project and run it. Once again, have in mind that my project uses the latest internal build assemblies from 24.07.2009.

Please, let me know if you have any other questions.

Regards,
Ross
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
John
Top achievements
Rank 1
answered on 17 Aug 2009, 08:35 PM
Ross,

I had a little bit of trouble running the example you provided under Silverlight 2. It appears that the binaries you included depend on a different version of the System.Windows dll.

Nevertheless, I loaded up the sample using the Q2.2009 (2009.2.701.1020) controls, and it seems that the GridView.Items collection is always empty. Is this a bug in this particular version of the control? If so, do you know of any work around? I'm not sure when we will be upgrading to the next version of controls...

The GridView.Records property seems to work but the only downside is the compiler warning associated with using the Record class. ('Telerik.Windows.Data.DataRecord' is obsolete: 'This class is obsolete and is not used anymore.) If there is no work around for the Items property issue, we may just have to deal with the compiler warning until an upgrade.

Thanks!
0
Vlad
Telerik team
answered on 19 Aug 2009, 07:02 AM
Hi John,

Indeed you are right about Items in Q2 - please download our latest service pack where this is fixed.

Greetings,
Vlad
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
Tags
GridView
Asked by
John
Top achievements
Rank 1
Answers by
Rossen Hristov
Telerik team
John
Top achievements
Rank 1
Vlad
Telerik team
Share this question
or