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

Self referencing hierarchy RowLoaded event on Child Table Definitions

2 Answers 130 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Russ
Top achievements
Rank 1
Russ asked on 29 Jun 2010, 08:25 PM
I've been working on an application using a multi-level self-referencing heirarchy, and have come across a problem with using the RowLoaded event for any level other than the first level.  Hopefully someone can give me a pointer on how to get this working. 

My basic goal at the moment is to disable the expand button (+) next to items that do not have a heirarchy beneath them,  I've recreated the sample from here in WPF, as a simple baseline.   I've added a little to this to try to make it work as I need (including only having heirarchies related to some items),  So I'll post my version of the code below.   As I mentioned, I'm using the RowLoaded event to decide whether or not to make the row expandable, but this only works for the first level. 

Is there a way I  can use this RowLoaded event (or another one) to do the same for levels 2 - 10 (or however deep it goes)?

Thanks,
Russell

The MainWindow.xaml:
<Window x:Class="HeirarchicalGridExample.MainWindow" 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        xmlns:telerikGrid="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.GridView" 
        xmlns:telerikData="clr-namespace:Telerik.Windows.Data;assembly=Telerik.Windows.Data" 
        Title="MainWindow" Height="350" Width="525">  
    <Grid> 
        <telerikGrid:RadGridView x:Name="RadGridView1" ItemsSource="{Binding}" DataLoading="RadGridView1_DataLoading" RowLoaded="RadGridView1_RowLoaded" /> 
    </Grid> 
</Window> 

The MainWindow.xaml.cs:
using System;  
using System.Collections.Generic;  
using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Windows;  
using System.Windows.Data;  
using Telerik.Windows.Controls.GridView;  
using Telerik.Windows.Controls;  
using Telerik.Windows.Data;  
 
namespace HeirarchicalGridExample  
{  
    /// <summary> 
    /// Interaction logic for MainWindow.xaml  
    /// </summary> 
    public partial class MainWindow : Window  
    {  
        public MainWindow()  
        {  
            InitializeComponent();  
            DataContext = from i in Enumerable.Range(0, 5)  
                          select new MyObject()  
                          {  
                              ID = i,  
                              Name = String.Format("Name{0}", i),  
                              Level = 1 
                          };  
        }  
 
        public class MyObject  
        {  
            public int ID { get; set; }  
            public string Name { get; set; }  
            public int Level { get; set; }  
 
            public IEnumerable<MyObject> Items  
            {  
                get  
                {  
                    if (ID == 1 || ID == 3 || ID == 5 || ID == 7)  
                    {  
                        return from i in Enumerable.Range(0, 10)  
                               select new MyObject()  
                               {  
                                   ID = i,  
                                   Name = String.Format("{0}", Name + " - " + i.ToString()),  
                                   Level = Level + 1  
                               };  
                    }  
                    else  
                        return Enumerable.Empty<MyObject>();  
                }  
            }  
        }  
 
        private void RadGridView1_DataLoading(object sender, Telerik.Windows.Controls.GridView.GridViewDataLoadingEventArgs e)  
        {  
            var grid = (GridViewDataControl)sender;  
 
            var d = new GridViewTableDefinition();  
            d.Relation = new PropertyRelation("Items");  
            grid.TableDefinition.ChildTableDefinitions.Add(d);  
 
            grid.AutoGenerateColumns = false;  
            grid.Columns.Add(new GridViewDataColumn() { DataMemberBinding = new Binding("Level") });  
            grid.Columns.Add(new GridViewDataColumn() { DataMemberBinding = new Binding("ID") });  
            grid.Columns.Add(new GridViewDataColumn() { DataMemberBinding = new Binding("Name") });  
        }  
 
        // this is only working for the top level of the grid  
        private void RadGridView1_RowLoaded(object sender, RowLoadedEventArgs e)  
        {  
            GridViewRow row = e.Row as GridViewRow;  
            MyObject rowItem = e.DataElement as MyObject;  
 
            if (row != null && rowItem != null)  
            {  
                if (rowItem.Items.Count<MyObject>() > 0)  
                    row.IsExpandable = true;  
                else  
                    row.IsExpandable = false;  
            }  
        }  
    }  
}  
 

2 Answers, 1 is accepted

Sort by
0
Accepted
Veselin Vasilev
Telerik team
answered on 02 Jul 2010, 09:39 AM
Hello Russ,

Yes, that is right. The RowLoaded event fires for the parent gridview only. If you need that event for the child (nested) grids you need to define a HierarchyChildTemplate, place a gridview inside it and subscribe to its RowLoaded event.

I am attaching a sample project here - hope it will help you.

Regards,
Veskoni
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
Russ
Top achievements
Rank 1
answered on 02 Jul 2010, 12:56 PM
Yes, Thank you.  I've just gone through your example and that seems to be exactly what I'm looking for.

Thanks for the help.

Russell.
Tags
GridView
Asked by
Russ
Top achievements
Rank 1
Answers by
Veselin Vasilev
Telerik team
Russ
Top achievements
Rank 1
Share this question
or