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

Getting the GridView Row for GroupRows

8 Answers 287 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Hector
Top achievements
Rank 1
Hector asked on 22 Jun 2009, 08:20 PM
I have a GridView which has grouping, and I would like to select all the selected rows or unselected rows and be able to hide them from view.  I have the code that uses ItemsGenerator to get the row as the specified index, however, it is returning an error because of the grouping.  When I ungroup it, it works fine.  This is the error I get - "Unable to cast object of type 'Telerik.Windows.Controls.GridView.GridViewGroupRow' to type 'Telerik.Windows.Controls.GridView.GridViewRow'." - , which clearly states I am trying to cast a GridViewGroupRow to a GridViewRow.  When I step through the code the DataRecord that I am returning the Index for is indeed a datarecord, not a grouprecord.  Is there something I am missing for Groups? Try this code on a grouped GridView, you will get the same error.

var row = this.gridMultiSelect_accounts.ItemsControl.ItemsGenerator.GenerateItemAtIndex(inx, out isNewlyCreated);  
 
if (row is GridViewGroupRow)  
{  
    GridViewGroupRow grouprow = (GridViewGroupRow)row;  
    var row2 = grouprow.ItemsGenerator.GenerateItemAtIndex(dr.DataSourceIndex);  
    foreach (DataRecord ddrr in grouprow.Records)  
    {  
        //How do I get the GridViewRow ???  
    }  

8 Answers, 1 is accepted

Sort by
0
Rossen Hristov
Telerik team
answered on 25 Jun 2009, 01:11 PM
Hello Hector,

I have tried to run this but I am missing several things. First of all, when and where is this code called? What are the values of inx and dr? Where do they come from?

It will be best if you send us a small sample project that can be compiled and run.

Also, I would like to ask you about the exact goal that you are after, since I don't think that using the ItemsGenerator is the most proper way to achieve it. Can you please describe in great detail what are you trying to achieve and we will try to come up with a better solution for it.

I am looking forward to hearing from you.

Sincerely yours,
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
Hector
Top achievements
Rank 1
answered on 25 Jun 2009, 03:06 PM
Hi Ross, thanks for your response.  Sorry on my first post, it was not very clear.  Here is what I am attempting to do:  The use selects rows from the GridView.  There is a comboBox pulldown where the user can choose to show only selected rows, show only unselected rows, or show all rows.  This is somewhat like adding a filter, but I was unable to find a solution using filters since the IsSelected property not part of the data object.  I created a simple project that shows exactly what I need.  It works flawlessly when the GridView does not have any grouped rows, but errors out when it encounters a GridViewGroupRow.  In this example, try running it with the grouped column; first select some rows and click on the pull-down.  You will encounter the said error.  Next, run it again, but remove the grouping and select rows.  Choose different options from the pull down and you will see it has the filtering effect just like I want it.  It is the grouping that is causing me the error.
Thank you very much for looking at this.
Since I wasn't able to upload, here is my code.
Page.xaml.cs
using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Net;  
using System.Windows;  
using System.Windows.Controls;  
using System.Windows.Documents;  
using System.Windows.Input;  
using System.Windows.Media;  
using System.Windows.Media.Animation;  
using System.Windows.Shapes;  
using Telerik.Windows.Data;  
using Telerik.Windows.Controls.GridView;  
 
namespace IterateRecordsSample  
{  
    public partial class Page : UserControl  
    {  
        public Page()  
        {  
            InitializeComponent();  
            this.RadGridView1.ItemsSource = Person.GetSampleListOfPersons();  
            this.RadGridView1.GroupDescriptions.Add(new RadGroupDescription("LastName"));  
        }  
 
        private void cmbShowRows_SelectionChanged(object sender, SelectionChangedEventArgs e)  
        {  
            if (cmbShowRows == null) return;  
            ComboBoxItem item = (ComboBoxItem) cmbShowRows.SelectedItem;  
            string selectedItem = item.Content.ToString();  
            if (selectedItem == "Filter by:") return;  
            bool isNewlyCreated = false;  
 
            // Now,  only make visible the rows the user chose by hiding the others  
            foreach (Record r in RadGridView1.Records)  
            {  
                if (r is GroupRecord)  
                {  
                    GroupRecord gr = (GroupRecord)r;  
                    foreach (DataRecord dr in gr.Records) //parse through Datarecords in group  
                    {  
                        // this next line fails if GridView is grouped  
                        GridViewRow row = (GridViewRow)this.RadGridView1.ItemsControl.ItemsGenerator.GenerateItemAtIndex(dr.DataSourceIndex, out isNewlyCreated);  
                        // if isSelectedVisible is true, then make the non selected set un-visible  
                        if (selectedItem == "Show selected")  
                        {  
                            row.Visibility = (row.IsSelected == true) ? Visibility.Visible : Visibility.Collapsed; //if  selected, make visible.Visible, otherwise collapsed  
                        }  
                        else if (selectedItem == "Show unselected")  
                        {  
                            row.Visibility = (row.IsSelected == false) ? Visibility.Collapsed : Visibility.Visible; //if not selected, make visible.collapsed, otherwise make visible  
                        }  
                        else //make all visible  
                            row.Visibility = Visibility.Visible;  
                    }  
                }  
                else if(r is DataRecord)  
                {  
                    DataRecord dr = (DataRecord)r;  
                    // this next line fails if GridView is grouped  
                    GridViewRow row = (GridViewRow)this.RadGridView1.ItemsControl.ItemsGenerator.GenerateItemAtIndex(dr.DataSourceIndex, out isNewlyCreated);  
                    // if isSelectedVisible is true, then make the non selected set un-visible  
                    if (selectedItem == "Show selected")  
                    {  
                        row.Visibility = (row.IsSelected == true) ? Visibility.Visible : Visibility.Collapsed; //if  selected, make visible.Visible, otherwise collapsed  
                    }  
                    else if (selectedItem == "Show unselected")  
                    {  
                        row.Visibility = (row.IsSelected == false) ? Visibility.Visible : Visibility.Collapsed; //if not selected, make visible.collapsed, otherwise make visible  
                    }  
                    else //make all visible  
                        row.Visibility = Visibility.Visible;  
                }  
            }  
        }  
 
        public class Person  
        {  
            public Person(int age, string firstName, string lastName)  
            {  
                this.Age = age;  
                this.FirstName = firstName;  
                this.LastName = lastName;  
            }  
 
            public int Age { get; set; }  
            public string FirstName { get; set; }  
            public string LastName { get; set; }  
 
            public static List<Person> GetSampleListOfPersons()  
            {  
                var persons = new List<Person> 
                            {  
                                new Person(30, "John", "Smith"),  
                                new Person(29, "Jane", "Smith"),  
                                new Person(30, "Peter", "Smith"),  
                                new Person(30, "John2", "Johnson"),  
                                new Person(29, "Jane2", "Johnson"),  
                                new Person(30, "Peter2", "Johnson"),  
                                new Person(30, "John3", "Rodriguez"),  
                                new Person(29, "Jane3", "Rodriguez"),  
                                new Person(30, "Peter3", "Rodriguez"),  
                                new Person(30, "John4", "Lee"),  
                                new Person(29, "Jane4", "Lee"),  
                                new Person(30, "Peter4", "Lee")  
                            };  
                return persons;  
            }  
        }  
    }  
}  
 

Page.xaml
<UserControl x:Class="IterateRecordsSample.Page" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"   
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"   
    xmlns:telerik="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.GridView" 
    Width="400" Height="Auto">  
    <Grid x:Name="LayoutRoot" Background="White">  
        <Grid.RowDefinitions> 
            <RowDefinition  Height="Auto"/>  
            <RowDefinition Height="30" /> 
        </Grid.RowDefinitions> 
        <telerik:RadGridView x:Name="RadGridView1"  MultipleSelect="True"/>  
        <ComboBox x:Name="cmbShowRows" SelectionChanged="cmbShowRows_SelectionChanged" Grid.Row="1">  
            <ComboBox.Items > 
                <ComboBoxItem  Content="Filter by:" IsSelected="True" /> 
                <ComboBoxItem Content="Show selected" /> 
                <ComboBoxItem Content="Show unselected" /> 
                <ComboBoxItem Content="Show all" /> 
            </ComboBox.Items> 
        </ComboBox> 
    </Grid> 
</UserControl> 
 
0
Accepted
Rossen Hristov
Telerik team
answered on 26 Jun 2009, 11:22 AM
Hello Hector,

You can use our extension method called ChildrenOfType<T> to locate all rows in the grid and manipulate them. To use this method you need to add a using directive to Telerik.Windows.Controls. Here is how I did it and it seems to be working according to your requirements:

        private void cmbShowRows_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)   
        {   
            if (cmbShowRows == nullreturn;   
            ComboBoxItem item = (ComboBoxItem) cmbShowRows.SelectedItem;   
            string selectedItem = item.Content.ToString();   
            if (selectedItem == "Filter by:"return
 
            IList<GridViewRow> rows = this.RadGridView1.ChildrenOfType<GridViewRow>(); 
            foreach (GridViewRow row in rows) 
            { 
                if (selectedItem == "Show selected"
                { 
                    row.Visibility = (row.IsSelected == true) ? Visibility.Visible : Visibility.Collapsed; //if  selected, make visible.Visible, otherwise collapsed   
                } 
                else if (selectedItem == "Show unselected"
                { 
                    row.Visibility = (row.IsSelected == false) ? Visibility.Visible : Visibility.Collapsed; //if not selected, make visible.collapsed, otherwise make visible   
                } 
                else //make all visible   
                    row.Visibility = Visibility.Visible;   
            } 
        }   
 

I hope this helps. Please, let me know if you have any other questions.

Kind 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
Hector
Top achievements
Rank 1
answered on 27 Jun 2009, 01:37 PM
Outstanding!  This way you don't even have to check if it is a DataRecord our GroupRecord!  Thank you so much, you guys are awesome!
0
Michele
Top achievements
Rank 2
answered on 07 Jul 2009, 03:05 PM
excuse me, does it applies to RadGridView in Q2??
I'm not able to find it..

thanks

Paolo
0
Rossen Hristov
Telerik team
answered on 08 Jul 2009, 12:45 PM
Hello Paolo,

With Q2 we have introduced a new data engine and and you should no longer work with the wrapper objects such as Record/DataRecord, but with the data items themselves, which is easier.

For the sake of backwards compatibility we have left the records, but they will be removed soon so you should not use them. Currently, we have pairs of properties such as SelectedItem/SelectedRecord, CurrentItem/CurrentRecord and so on. You should be working with the ***Item ones.

You can take a look at our updated example about selection at the following address - http://demos.telerik.com/silverlight/#GridView/Selection

Please let me know if you have any other questions.

Sincerely yours,
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
Michael Rodriguez
Top achievements
Rank 1
answered on 26 Mar 2010, 09:04 PM
Guys, seriously, how in the world does your grid not have a Rows property?

    public IList<GridViewRow> Rows
    {
      get
      {
        return this.ChildrenOfType<GridViewRow>();
      }
      set
      {
        var rows = this.ChildrenOfType<GridViewRow>();
        rows = value;
      }
    }

Do you have any idea how long it took us to find that?  Your grid does so many great things, to not expose a basic Rows property is amazing to me.



0
Vlad
Telerik team
answered on 29 Mar 2010, 07:22 AM
Hi,

RadGridView is very similar to standard Silverlight/WPF DataGrid and due to stuff like rows virtualization and container recycling such property do not exist.

Sincerely yours,
Vlad
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.
Tags
GridView
Asked by
Hector
Top achievements
Rank 1
Answers by
Rossen Hristov
Telerik team
Hector
Top achievements
Rank 1
Michele
Top achievements
Rank 2
Michael Rodriguez
Top achievements
Rank 1
Vlad
Telerik team
Share this question
or