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

how to bind data to display hierarchy (visual basic)

2 Answers 96 Views
TreeListView
This is a migrated thread and some comments may be shown as answers.
Robert
Top achievements
Rank 1
Robert asked on 22 Oct 2011, 12:18 AM
Hi there,

I'm having difficulty figuring out how to bind my data ( ObservableCollection(of) ) and to display it as a hierarchy using vb.

I have the following example data, similar to your C# example I saw in another post called Treelistview-Self-Hierarchy.

ID             Name               ParentID
1               John                 NULL                   ** Top level node **
2               Chris                1                      ** Parent is John **
3               Simon                NULL                   ** Top level node **
4               Dave                 1                      ** parent is John **
5               Joe                  3                      ** parent is simon **

** Please note again that the above is an ObservableCollection(Of ) list.

I have the following XAML List view.

<telerik:RadTreeListView x:Name="ParksTreeListView" AutoGenerateColumns="False" IsFilteringAllowed="True" IsBusy="False">
    <telerik:RadTreeListView.Columns>
        <telerik:GridViewDataColumn DataMemberBinding="{Binding ID}"
                            Header="ID" />
        <telerik:GridViewDataColumn DataMemberBinding="{Binding Name}"
                            Header="Name" />
        <telerik:GridViewDataColumn DataMemberBinding="{Binding ParentID}"
                            Header="Parent" />
    </telerik:RadTreeListView.Columns>
</telerik:RadTreeListView>

I then set the ItemsSource in VB:

'Get parks information from SQL database
Dim parksList As ObservableCollection(Of ParksTree) = ParksDB.GetAllParks()
  
'Set Tree items source
ParksTreeListView.ItemsSource = parksList

This displays all the information correctly in grid. However I need to group the items according to it's hierarchy.How can I achieve this with the minimal of fuss?
** Please note I am a newbie! **

Out of interest, the hierachy in my SQL 2008 database uses the new hierarchyid data type. Can the TreeListView use the hierarchyid data directly to generate the tree nodes?

2 Answers, 1 is accepted

Sort by
0
Vlad
Telerik team
answered on 24 Oct 2011, 07:39 AM
Hello,

 You need to create new property for your data item representing the child items and bind this property using TreeListViewTableDefinition. 

Please check our demos and documentation for more info!

Greetings,
Vlad
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

0
Robert
Top achievements
Rank 1
answered on 24 Oct 2011, 07:43 PM

I take it this is where I should be looking?

Namespace Telerik.Windows.Examples.TreeListView.OnDemandDataLoading
    Public Class FolderViewModel
        Inherits ViewModelBase
        Private m_name As String
        Private m_isEmpty As Boolean
        Private m_items As ObservableCollection(Of FolderViewModel)
        Private folderElement As XElement
  
        Public Sub New(name As String, isEmpty As Boolean, element As XElement)
            Me.Name = name
            Me.IsEmpty = isEmpty
            Me.folderElement = element
        End Sub
  
        Public Property Name() As String
            Get
                Return Me.m_name
            End Get
            Private Set
                Me.m_name = value
            End Set
        End Property
  
        Public Property IsEmpty() As Boolean
            Get
                Return Me.m_isEmpty
            End Get
            Private Set
                Me.m_isEmpty = value
            End Set
        End Property
  
        Public Property Items() As ObservableCollection(Of FolderViewModel)
            Get
                Return Me.m_items
            End Get
            Private Set
                If Me.m_items IsNot value Then
                    Me.m_items = value
  
                    OnPropertyChanged("Items")
                End If
            End Set
        End Property
  
        Public Sub LoadChildren()
            If Me.Items Is Nothing Then
                Me.Items = New ObservableCollection(Of FolderViewModel)(From f In Me.folderElement.Elements("folder") _
                    Select New FolderViewModel(f.Attribute("name").Value, CBool(f.Attribute("isEmpty")), f))
            End If
        End Sub
    End Class
End Namespace

Am I right in thinking that "items" are actually another observable collection list contained in a column of the parent list?
Interesting, it never occurred to me to embed a list inside another list column.

Tags
TreeListView
Asked by
Robert
Top achievements
Rank 1
Answers by
Vlad
Telerik team
Robert
Top achievements
Rank 1
Share this question
or