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

Help with Hierarchical DataBind needed

26 Answers 310 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Andrew
Top achievements
Rank 1
Veteran
Iron
Andrew asked on 08 Oct 2010, 08:38 PM
I have a list of dataobjects of type 'Package' that I am binding to the grid (works great)
The 'Package' object as property called 'evaluations' that contains a list of type 'Evaluation' that are children of the package

The method that returns the list of 'Packages' is what is being bound to grid.DataSource.

How do I get the items from the property 'evaluation' to show up a child ros in the grid (List Package = Master, List Evaluation = Detail)

I hope this is clear, if not let me know an I will try to explain more clearly


Thanks,
Andrew

26 Answers, 1 is accepted

Sort by
0
Richard Slade
Top achievements
Rank 2
answered on 08 Oct 2010, 10:28 PM
Hi,

You'll need to define an object source for the master template, and then add a template for the child data source. After this you will need to define a relation between the two. For exmaple. The ParentId (on the parent - PK) and the ParentId on the child (FK).

There is a good article here regarding binding to hierarchical data.

Let me know if you need more help
Richard
0
Andrew
Top achievements
Rank 1
Veteran
Iron
answered on 12 Oct 2010, 06:29 PM
I need more help,

I tried the help article, but I am dealing with a collection of business objects (master data) and each object has a property that is another collection of business objects (child data)

I tried selecting data source type of object, and I assigned business objects that will be returned to each data source, but I cannot get this to relate, I see the plus sign in the grid but there is no child data, I am guess it does not know where to look to get the child data object from the master property on the master data object.

Here are my business objects

Master:
using System;
using System.Collections.Generic;
  
namespace Ivey.Components.CourseEvals.BusLayer
{
  
    /// <summary>
    /// 
    /// </summary>
    public class Package
    {
  
        #region Variables
  
            private Guid id;
            private string fileName;
            private DateTime dateCreated;
            private Guid createdBy;
            private DateTime dateSent;
            private Guid sentBy;
            private List<EvaluationsAvailable> evaluations = new List<EvaluationsAvailable>();
          
        #endregion
  
        #region Public Properties
  
            /// <summary>
            /// Gets or sets the id.
            /// </summary>
            /// <value>The id.</value>
            public Guid Id
            {
                set { id = value; }
                get { return id; }
            }
  
            /// <summary>
            /// Gets or sets the name of the file.
            /// </summary>
            /// <value>The name of the file.</value>
            public string FileName
            {
                set { fileName = value; }
                get { return fileName; }
            }
  
            /// <summary>
            /// Gets or sets the date created.
            /// </summary>
            /// <value>The date created.</value>
            public DateTime DateCreated
            {
                set { dateCreated = value; }
                get { return dateCreated; }
            }
  
            /// <summary>
            /// Gets or sets the created by.
            /// </summary>
            /// <value>The created by.</value>
            public Guid CreatedBy
            {
                set { createdBy = value; }
                get { return createdBy; }
            }
  
            /// <summary>
            /// Gets or sets the date sent.
            /// </summary>
            /// <value>The date sent.</value>
            public DateTime DateSent
            {
                set { dateSent = value; }
                get { return dateSent; }
            }
  
            /// <summary>
            /// Gets or sets the sent by.
            /// </summary>
            /// <value>The sent by.</value>
            public Guid SentBy
            {
                set { sentBy = value; }
                get { return sentBy; }
            }
  
            /// <summary>
            /// Gets or sets the evaluations.
            /// </summary>
            /// <value>The evaluations.</value>
            public List<EvaluationsAvailable> Evaluations
            {
                set { evaluations = value; }
                get { return evaluations; }
            }
          
        #endregion
    }
}


Child:
using System;
  
  
namespace Ivey.Components.CourseEvals.BusLayer{
    public class EvaluationsAvailable
    {
        /// <summary>
        /// 
        /// </summary>
        private DateTime _StartDate = DateTime.MinValue;
        private DateTime _EndDate = DateTime.MinValue;
        private string _CourseExact = string.Empty;
        private Guid _CourseEvalsAvailableId = Guid.Empty;
        private Guid _CourseEvalsPackageId = Guid.Empty;
  
        /// <summary>
        /// Gets or sets the start date.
        /// </summary>
        /// <value>The start date.</value>
        public DateTime StartDate
        {
            get { return _StartDate; }
            set { _StartDate = value; }
        }
  
        /// <summary>
        /// Gets or sets the end date.
        /// </summary>
        /// <value>The end date.</value>
        public DateTime EndDate
        {
            get { return _EndDate; }
            set { _EndDate = value; }
        }
  
        /// <summary>
        /// Gets or sets the course exact.
        /// </summary>
        /// <value>The course exact.</value>
        public string CourseExact
        {
            get { return _CourseExact; }
            set { _CourseExact = value; }
        }
  
        /// <summary>
        /// Gets or sets the course available id.
        /// </summary>
        /// <value>The course available id.</value>
        public Guid CourseEvalsAvailableId
        {
            get { return _CourseEvalsAvailableId; }
            set { _CourseEvalsAvailableId = value; }
        }
  
        /// <summary>
        /// Gets or sets the course evals package id.
        /// </summary>
        /// <value>The course evals package id.</value>
        public Guid CourseEvalsPackageId
        {
            get { return _CourseEvalsPackageId; }
            set { _CourseEvalsPackageId = value; }
        }
  
    }
}
 
this property on master data object contains the child data
public List<EvaluationsAvailable> Evaluations



0
Emanuel Varga
Top achievements
Rank 1
answered on 12 Oct 2010, 06:46 PM
Hello Andrew,

Currently (Q2 2010 SP2) there is no way to bind hierarchies of business objects with self contained collections to the grid directly, but from what i understand they are working on an Object-relational hierarchy which will cover your scenario.
This feature is said to be scheduled for Q3 2010 release.

But, if you can add a PackageId inside the Evaluation (and you can remove the list from inside Packages) everything will work out fast and easy, please take a look at this thread if you decide that this is acceptable, at least until the Q3 release, which should be right around the corner.

Hope this helps, if you have any other questions or comments, please let me know,

Best Regards,
Emanuel Varga

0
Andrew
Top achievements
Rank 1
Veteran
Iron
answered on 13 Oct 2010, 03:08 PM
Ok, I tried this example as above but I do not get a child grid when I try to expand the row to show the child grid

here is my code:
CourseEvalsBF.Packages packagesBF = new CourseEvalsBF.Packages(_dalCourseEvalsProvider);
                //rgvPackages.DataSource = packages.GetAll(true);
  
                //Get Packages / Extract Evaluations From Packages
                List<Package> packages = packagesBF.GetAll(true);
                List<EvaluationsAvailable> evaluations = GetEvaluationsFromPackages(packages);
  
                //Load Primary Data
                rgvPackages.AutoGenerateHierarchy = true;
                rgvPackages.DataSource = packages;
                //rgvPackages.BestFitColumns();
  
                //Create Relation Template
                GridViewTemplate evaluationTemplate = new GridViewTemplate();
                evaluationTemplate.DataSource = evaluations;
  
                GridViewRelation evaluationRelation = new GridViewRelation(rgvPackages.MasterTemplate);
                evaluationRelation.ChildTemplate = evaluationTemplate;
                evaluationRelation.RelationName = "ParentChild";
                evaluationRelation.ParentColumnNames.Add("Id");
                evaluationRelation.ChildColumnNames.Add("CourseEvalsPackageId");
                rgvPackages.Relations.Add(evaluationRelation);
                evaluationTemplate.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
                evaluationTemplate.BestFitColumns();
                evaluationTemplate.Caption = "Evaluations";
  
                rgvPackages.MasterTemplate.Templates.Add(evaluationTemplate);
                rgvPackages.MasterTemplate.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;



is there a projected date for release this Q3 release?

I was just wondering is there an event that I could use to set the datasource for the child grid manually
0
Emanuel Varga
Top achievements
Rank 1
answered on 13 Oct 2010, 03:46 PM
Hello again,

1. Can you please post a stack trace or some more information abut the null reference, I've tried to replicate it in my example but could not. Which version of telerik controls are you using? I'm using Q2 2010 SP2. Please try to run the example i've posted there and see if everything works for that, if everything is OK for that i will try to help you find the problem.
2. Time frame for Q3 sorry, don't know yet.
3. There is no event to manually set the data source for the child grid.

Best Regards,
Emanuel Varga
0
Andrew
Top achievements
Rank 1
Veteran
Iron
answered on 13 Oct 2010, 03:58 PM
I can get the example to work.

I think the null issue was that I had some other templates and releationships that I created via the ui still existing so I have removed then and that issue has gone away, but now I do not get any child table when I click on the '+'

my relationship looks fine to me

0
Emanuel Varga
Top achievements
Rank 1
answered on 13 Oct 2010, 04:33 PM
Hello Andrew,

Please check the column names for both the Parent and the Childs, something is wrong there if no data is shown, I've changed that example to use Guids, but it's working like that also with no problems.

Best Regards,
Emanuel Varga
0
Andrew
Top achievements
Rank 1
Veteran
Iron
answered on 13 Oct 2010, 04:37 PM
I have done that, if you look at my class examples the parent should be Package.Id  and it should relate to EvaluationsAvailable.CourseEvalsPackageId
0
Andrew
Top achievements
Rank 1
Veteran
Iron
answered on 13 Oct 2010, 05:11 PM
I got it to work, but I had to remove the columns that I had setup via the UI on the master, why would that be?
0
Andrew
Top achievements
Rank 1
Veteran
Iron
answered on 13 Oct 2010, 05:16 PM
just to verify the above when I added a column of datetime via the UI, my child grid will no longer display
0
Accepted
Emanuel Varga
Top achievements
Rank 1
answered on 13 Oct 2010, 05:27 PM
Hello Andrew,

I tried adding the column to the MasterTemplate programmatically and it worked, I added a new column from the designer and it worked, maybe the designer is creating something wrong somewhere, but without seeing your code, i can't tell, try adding the columns to the master template from code and let me know what happens

Best Regards,
Emanuel Varga
0
Andrew
Top achievements
Rank 1
Veteran
Iron
answered on 13 Oct 2010, 08:25 PM
ok, I got it to work, I did not realize that the column that the relation is on must exist as a column in the grid, so I added it and and set isvisible to false.

Thanks,
for the help 

 




0
Emanuel Varga
Top achievements
Rank 1
answered on 13 Oct 2010, 08:49 PM
Hello again,

For the first part, yes that column has to be part of the grid, but if you don't want it to be visible you can just set MinWidth,MaxWidth, to 1, NOT 0.

Second one, try this:
cell.CommandButton.TextElement.StretchHorizontally = true;
cell.CommandButton.TextElement.StretchVertically = true;

*Update: add this also to center the text:
commandCell.CommandButton.TextElement.TextAlignment = ContentAlignment.MiddleCenter;

If you have any other questions or comments, please let me know,

Best Regards,
Emanuel Varga
0
Alexander
Telerik team
answered on 14 Oct 2010, 08:48 AM
Hello,

The Q3 2010 release is scheduled for November. As Emanuel has explained, the object-relational hierarchy will be a new feature in it. It will allow you to implement your initial scenario without adding the additional property CourseEvalsPackageID in the Evaluation class to define the relation.

Do not hesitate to write back if you have more questions.

Best regards,
Alexander
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
Cat
Top achievements
Rank 1
answered on 30 Nov 2010, 11:45 PM
So it's November now, almost December. Was this capability added, and is there documentation on how it works? It would be lovely if this were available!
0
Richard Slade
Top achievements
Rank 2
answered on 30 Nov 2010, 11:54 PM
Hello Cat,

Yes, this is now available. Have a look at the following links which will help.

Q3 2010 object-relational hierarchy with radgridview for winforms
binding to self referncing data (Telerik TV)

Hope this helps but let me know if you need more information
richard
0
Cat
Top achievements
Rank 1
answered on 01 Dec 2010, 12:21 AM
I'm attempting to get the following simple (as in simplest worst-case) example working. The hierarchy isn't showing up. :( If you could point me in the right direction it would make my day!


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls.UI;
  
namespace RadGridViewExample
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
  
            gvGridView.DataSource = Data.TheData;
            gvGridView.ReadOnly = true;
            gvGridView.AutoGenerateHierarchy = true;
        }
    }
  
    public class LevelOneData
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public List<string> Locations { get; set; }
        public List<LevelTwoData> LevelTwoCollection { get; set; }
    }
  
    public class LevelTwoData
    {
        public string Title { get; set; }
        public List<LevelThreeData> LevelThreeDataCollection { get; set; }
    }
  
    public class LevelThreeData
    {
        public int L3ID { get; set; }
        public string Identity { get; set; }
    }
  
    public static class Data
    {
        public static List<LevelOneData> TheData = new List<LevelOneData>
        {
            new LevelOneData
            {
                ID = 0,
                Name = "First Name",
                Locations = new List<string>
                {
                    "Loc 1",
                    "Loc 2",
                    "Loc 3",
                    "New Location"
                },
                LevelTwoCollection = new List<LevelTwoData>
                {
                    new LevelTwoData
                    {
                        Title = "Level 2 Title",
                        LevelThreeDataCollection = new List<LevelThreeData>
                        {
                            new LevelThreeData
                            {
                                L3ID = 100,
                                Identity = "L3 Identity"
                            },
                            new LevelThreeData
                            {
                                L3ID = 101,
                                Identity = "L3 Identity 2"
                            },
                        }
                    },
                    new LevelTwoData
                    {
                        Title = "Level 2 Title Again",
                        LevelThreeDataCollection = new List<LevelThreeData>
                        {
                            new LevelThreeData
                            {
                                L3ID = 103,
                                Identity = "L3 Identity 3"
                            },
                            new LevelThreeData
                            {
                                L3ID = 104,
                                Identity = "L3 Identity 4"
                            },
                        }
                    }
                }
            },
            new LevelOneData
            {
                ID = 1,
                Name = "Second Name",
                Locations = new List<string>
                {
                    "Loc 4",
                    "Loc 5",
                    "Loc 6"
                },
                LevelTwoCollection = new List<LevelTwoData>
                {
                    new LevelTwoData
                    {
                        Title = "Level 2 Title, part 2",
                        LevelThreeDataCollection = new List<LevelThreeData>
                        {
                            new LevelThreeData
                            {
                                L3ID = 105,
                                Identity = "L3 Identity"
                            },
                            new LevelThreeData
                            {
                                L3ID = 106,
                                Identity = "L3 Identity 2"
                            },
                        }
                    },
                    new LevelTwoData
                    {
                        Title = "Level 2 Title, Yet Again",
                        LevelThreeDataCollection = new List<LevelThreeData>
                        {
                            new LevelThreeData
                            {
                                L3ID = 107,
                                Identity = "L3 Identity 3"
                            },
                            new LevelThreeData
                            {
                                L3ID = 108,
                                Identity = "L3 Identity 4"
                            },
                        }
                    }
                }
            }
        };
    }
}
0
Richard Slade
Top achievements
Rank 2
answered on 01 Dec 2010, 01:34 AM
Hi Cat,

The AutoGenerateHierarchy is only available at the moment with Datasets as per this documentation.
You'd need to define a template and relation to bind object based data.

Hope that helps
Richard
0
Cat
Top achievements
Rank 1
answered on 01 Dec 2010, 05:34 PM
Hi Richard,
Thank you for the response. Does that mean that it's still not possible to do relational objects? I tried for a good part of yesterday to get the example I posted working, using all sorts of relationships and templates. This link that you provided (http://blogs.telerik.com/blogs/posts/10-11-13/q3_2010_object-relational_hierarchy_with_radgridview_for_winforms.aspx) shows a linq query that creates an auto-generated hierarchy off an object list, but I can't seem to get that to work with my code. I've also tried setting up very simple relationship and templates for a two level hierarchy, but I can't seem to get that to work either. Can you (or someone else) provide a very simple example (preferably similar to my own code) that shows how to get a hierarchy setup using relational objects? If this isn't the best place for this request, I can start a new thread too. I think my request is still exactly the same as the original poster though, trying to get relation objects working without using id columns, if possible. I keep reading that this is now possible (Q3), but I have yet to get it to work. :(

-Cat
0
Richard Slade
Top achievements
Rank 2
answered on 01 Dec 2010, 05:47 PM
Hi Cat,

Here is a really simple exmaple of binding to some object based data. It's just a grid on a form. 

Apologies it's in VB, but I thought it was better that you have it sooner rather than wait for me to translate it. If you need any further help, just let me know.

Best regards,
Richard

Imports System.Collections.Generic
Imports Telerik.WinControls
Imports Telerik.WinControls.UI
  
Public Class Form1
  
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  
        Dim parentList As New List(Of Parent)
        parentList.Add(New Parent(1, "Bob"))
        parentList.Add(New Parent(2, "Mary"))
        parentList.Add(New Parent(3, "George"))
        parentList.Add(New Parent(4, "Hilda"))
  
        Me.RadGridView1.DataSource = parentList
        Me.RadGridView1.AutoGenerateHierarchy = True
        Me.RadGridView1.Columns("Children").IsVisible = False
  
    End Sub
  
End Class
  
#Region "Parent Class"
Public Class Parent
    Private m_ParentId As Integer
    Private m_ParentName As String
  
    Public Sub New(ByVal id As Integer, ByVal name As String)
        m_ParentId = id
        m_ParentName = name
    End Sub
  
    Public Property ParentId() As Integer
        Get
            Return m_ParentId
        End Get
        Set(ByVal value As Integer)
            m_ParentId = value
        End Set
    End Property
  
    Public Property ParentName() As String
        Get
            Return m_ParentName
        End Get
        Set(ByVal value As String)
            m_ParentName = value
        End Set
    End Property
  
    Public ReadOnly Property Children() As List(Of Child)
        Get
            If Me.m_ParentId = 1 Then
                Dim childList As New List(Of Child)
                childList.Add(New Child(1, 1, "Baby Bob"))
                childList.Add(New Child(1, 2, "Little Bob"))
                childList.Add(New Child(1, 3, "Tiny Bob"))
                Return childList
            Else
                Return Nothing
            End If
        End Get
    End Property
End Class
#End Region
  
#Region "Child Class"
Public Class Child
    Private m_ParentId As Integer
    Private m_ChildId As Integer
    Private m_ChildName As String
  
    Public Sub New(ByVal parentId As Integer, ByVal id As Integer, ByVal name As String)
        m_ParentId = parentId
        m_ChildId = id
        m_ChildName = name
    End Sub
  
    Public Property ChildId() As Integer
        Get
            Return m_ChildId
        End Get
        Set(ByVal value As Integer)
            m_ChildId = value
        End Set
    End Property
  
    Public Property ParentId() As Integer
        Get
            Return m_ParentId
        End Get
        Set(ByVal value As Integer)
            m_ParentId = value
        End Set
    End Property
  
    Public Property ChildName() As String
        Get
            Return m_ChildName
        End Get
        Set(ByVal value As String)
            m_ChildName = value
        End Set
    End Property
End Class
#End Region
0
Cat
Top achievements
Rank 1
answered on 01 Dec 2010, 06:09 PM
Hi Richard,
Thank you again for your quick response and help. I'll translate and give that a try as soon as I get out of my current meeting. The object ID properties are not necessary for this example to work, correct? I ask as my own business objects may or may not have IDs associated with them.

-Cat
0
Richard Slade
Top achievements
Rank 2
answered on 01 Dec 2010, 06:17 PM
Hi Cat,

I don't think they are needed no. I tried this by commenting out the line that assigns the m_ParentId and it still works fine.

Public Sub New(ByVal parentId As Integer, ByVal id As Integer, ByVal name As String)
    'm_ParentId = parentId
    m_ChildId = id
    m_ChildName = name
End Sub

hope that helps
Richard
0
Cat
Top achievements
Rank 1
answered on 01 Dec 2010, 08:13 PM

Okay, I converted the code you provided into C# (seen below). It still doesn't show me anything other than the top level object list in the gridview. I am using version 2010.2.10.914 of the Telerik libs. What am I missing??

using System.Collections.Generic;
  
using System.Windows.Forms;
  
  
namespace RadGridViewExample
{
    public partial class Form1 : Form
    {
        List<Parent> TheData = new List<Parent>
        {
            new Parent
            {
                ID = 0,
                Name = "First Name",
                Children = new List<Child>
                {
                    new Child
                    {
                        ID = 0,
                        Title = "Level 2 Title"
                    },
                    new Child
                    {
                        ID = 11,
                        Title = "Level 2 Title Again"
                    }
                }
            },
            new Parent
            {
                ID = 1,
                Name = "Second Name",
                Children = new List<Child>
                {
                    new Child
                    {
                        ID = 12,
                        Title = "Level 2 Title",
                    },
                    new Child
                    {
                        ID = 13,
                        Title = "Level 2 Title Again"
                    }
                }
            }
        };
  
        public Form1()
        {
            InitializeComponent();
  
            gvGridView.DataSource = TheData;
            gvGridView.ReadOnly = true;
            gvGridView.AutoGenerateHierarchy = true;
            gvGridView.Columns["Children"].IsVisible = false;
        }
    }
  
    public class Parent
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public List<Child> Children { get; set; }
    }
  
    public class Child
    {
        public int ID { get; set; }
        public string Title { get; set; }
    }
  
}
0
Richard Slade
Top achievements
Rank 2
answered on 01 Dec 2010, 08:43 PM
Hello Cat,

As Alexander explained in his post above, binding to object based hierarcical data is a new feature in 2010 Q3. I suggest you download the latest version from your product area, or if you can't at the moment, download the trial version and give it a go with that.

Hope that helps
Richard
0
Cat
Top achievements
Rank 1
answered on 01 Dec 2010, 08:54 PM
I just realized that I had answered my own question with that post. I'm installing now and expect to have my sanity back within the hour. Thanks again for all your help Richard.

-Cat
0
Richard Slade
Top achievements
Rank 2
answered on 01 Dec 2010, 09:07 PM
You're welcome Cat. Anything else, just let me know.
Richard
Tags
GridView
Asked by
Andrew
Top achievements
Rank 1
Veteran
Iron
Answers by
Richard Slade
Top achievements
Rank 2
Andrew
Top achievements
Rank 1
Veteran
Iron
Emanuel Varga
Top achievements
Rank 1
Alexander
Telerik team
Cat
Top achievements
Rank 1
Share this question
or