Do not use on any
shared computer
August 30, 2008 03:31am pdt
 
[x]
Attachment Details

Handling Hierarchical Data with a TreeView in ASP.NET

I have been experimenting with methods to fill a TreeView from a database resultset and came upon what might be the general solution, at least for my current needs.  So,  it is in C# and I thought I would port it to VB.NET myself.  It turned out to not be a simple translation and I quickly ran out of energy to translate and research to translate amd found several C# to VB translators that appeared to do a good job, but when I added the translated code to a new class module,  I was presented with a dozen errors about needing to implement methods that appeared to me to be implemented.

I have attached the entire classs as a snippet.  If you cut and past this into a class file,  you will see the squiggles and error message.

Thanks.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
Imports System
Imports System.Data
Imports System.Web.UI
Imports System.Collections
 
'*********************************************************************** 
' Written By Ralph Varjabedian 
' You may use this code freely and copy this code provided that 
' You do not remove this copyright notice. 
' April 2008 
' http://www.varjabedian.net/archive/2008/04/22/binding-asp.net-treeview-to-a-dataset-or-an-objectdatasource.aspx 
'*********************************************************************** 
 
Namespace TreeViewBindingTest
    ''' <summary> 
    ''' A class that translates a DataSet into IHierarchicalDataSource that can be used to bind Hierarchical data to a TreeView 
    ''' </summary> 
    Public Class HierarchicalDataSet
        Implements IHierarchicalDataSource
        Private dataSet As DataSet
        Private idColumnName As String
        Private parentIdColumnName As String
 
        ''' <summary> 
        ''' The constructor of the class 
        ''' </summary> 
        ''' <param name="dataSet">The dataset that contains the data</param> 
        ''' <param name="idColumnName">The Primary key column name</param> 
        ''' <param name="parentidColumnName">The Parent Primary key column name that identifies the Parent-Child relationship</param> 
        Public Sub New(ByVal dataSet As DataSet, ByVal idColumnName As String, ByVal parentIdColumnName As String)
            Me.dataSet = dataSet
            Me.idColumnName = idColumnName
            Me.parentIdColumnName = parentIdColumnName
        End Sub
 
        Public Event DataSourceChanged(ByVal sender As Object, ByVal e As EventArgs)
        ' never used here 
 
        Public Function GetHierarchicalView(ByVal viewPath As String) As HierarchicalDataSourceView
            Return New DataSourceView(Me, viewPath)
        End Function
 
#Region "supporting methods"
        Private Function GetParentRow(ByVal row As DataRowView) As DataRowView
            dataSet.Tables(0).DefaultView.RowFilter = [String].Format("{0} = {1}", idColumnName, row(parentIdColumnName).ToString())
            Dim parentRow As DataRowView = dataSet.Tables(0).DefaultView(0)
            dataSet.Tables(0).DefaultView.RowFilter = ""
            Return parentRow
        End Function
 
        Private Function GetChildrenViewPath(ByVal viewPath As String, ByVal row As DataRowView) As String
            Return viewPath + "\" + row(idColumnName).ToString()
        End Function
 
        Private Function HasChildren(ByVal row As DataRowView) As Boolean
            dataSet.Tables(0).DefaultView.RowFilter = [String].Format("{0} = {1}", parentIdColumnName, row(idColumnName))
            Dim hasChildren As Boolean = dataSet.Tables(0).DefaultView.Count > 0
            dataSet.Tables(0).DefaultView.RowFilter = ""
            Return hasChildren
        End Function
 
        Private Function GetParentViewPath(ByVal viewPath As String) As String
            Return viewPath.Substring(0, viewPath.LastIndexOf("\"))
        End Function
#End Region
 
#Region "private classes that implement further interfaces"
        Private Class DataSourceView
            Inherits HierarchicalDataSourceView
            Private hDataSet As HierarchicalDataSet
            Private viewPath As String
 
            Public Sub New(ByVal hDataSet As HierarchicalDataSet, ByVal viewPath As String)
                Me.hDataSet = hDataSet
                Me.viewPath = viewPath
            End Sub
 
            Public Overloads Overrides Function [Select]() As IHierarchicalEnumerable
                Return New HierarchicalEnumerable(hDataSet, viewPath)
            End Function
        End Class
 
        Private Class HierarchicalEnumerable
            Implements IHierarchicalEnumerable
            Private hDataSet As HierarchicalDataSet
            Private viewPath As String
 
            Public Sub New(ByVal hDataSet As HierarchicalDataSet, ByVal viewPath As String)
                Me.hDataSet = hDataSet
                Me.viewPath = viewPath
            End Sub
 
            Public Function GetHierarchyData(ByVal enumeratedItem As Object) As IHierarchyData
                Dim row As DataRowView = DirectCast(enumeratedItem, DataRowView)
                Return New HierarchyData(hDataSet, viewPath, row)
            End Function
 
            Public Function GetEnumerator() As IEnumerator
                If viewPath = "" Then
                    hDataSet.dataSet.Tables(0).DefaultView.RowFilter = [String].Format("{0} is null", hDataSet.parentIdColumnName)
                Else
                    Dim lastID As String = viewPath.Substring(viewPath.LastIndexOf("\") + 1)
                    hDataSet.dataSet.Tables(0).DefaultView.RowFilter = [String].Format("{0} = {1}", hDataSet.parentIdColumnName, lastID)
                End If
 
                Dim i As IEnumerator = hDataSet.dataSet.Tables(0).DefaultView.GetEnumerator()
                hDataSet.dataSet.Tables(0).DefaultView.RowFilter = ""
                Return i
            End Function
        End Class
 
        Private Class HierarchyData
            Implements IHierarchyData
            Private hDataSet As HierarchicalDataSet
            Private row As DataRowView
            Private viewPath As String
 
            Public Sub New(ByVal hDataSet As HierarchicalDataSet, ByVal viewPath As String, ByVal row As DataRowView)
                Me.hDataSet = hDataSet
                Me.viewPath = viewPath
                Me.row = row
            End Sub
 
            Public Function GetChildren() As IHierarchicalEnumerable
                Return New HierarchicalEnumerable(hDataSet, hDataSet.GetChildrenViewPath(viewPath, row))
            End Function
 
            Public Function GetParent() As IHierarchyData
                Return New HierarchyData(hDataSet, hDataSet.GetParentViewPath(viewPath), hDataSet.GetParentRow(row))
            End Function
 
            Public ReadOnly Property HasChildren() As Boolean
                Get
                    Return hDataSet.HasChildren(row)
                End Get
            End Property
 
            Public ReadOnly Property Item() As Object
                Get
                    Return row
                End Get
            End Property
 
            Public ReadOnly Property Path() As String
                Get
                    Return viewPath
                End Get
            End Property
 
            Public ReadOnly Property Type() As String
                Get
                    Return GetType(DataRowView).ToString()
                End Get
            End Property
        End Class
#End Region
    End Class
End Namespace
Start your free trial to view this solution
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

Question Stats
Zone: Programming
Question Asked By: hbash
Solution Provided By: Russ_Suter
Participating Experts: 1
Solution Grade: A
Views: 65
Translate:
Loading Advertisement...
 
[+][-]Expert Comment by Russ_Suter
Expert Comment by Russ_Suter:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Accepted Solution by Russ_Suter
Accepted Solution by Russ_Suter:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Author Comment by hbash
Author Comment by hbash:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Expert Comment by Russ_Suter
Expert Comment by Russ_Suter:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Author Comment by hbash
Author Comment by hbash:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Loading Advertisement...
20080723-EE-VQP-34 / EE_QW_2_20070628