Click here to Skip to main content
15,890,579 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
This block of code is hard-coded to populate my Tasks TreeView down to three levels. However, my list of Tasks could have "n" levels in the hierarchy and I want to get rid of the hard-coding and improve flexibility. My code:

VB
TasksHierarchy.Nodes.Clear()
'populate treeview - add all top-level tasks
For Each dr As DataRow In DataSet1.DataTable1
   If dr.Item("ParentTaskID").Equals(DBNull.Value) Then 
   'task is at the top level
      TasksHierarchy.Nodes.Add("Task: " & dr.Item("TaskName") & "Task Type: " & dr.Item("TaskTypeName") & " - Owner: " & dr.Item("Name"))
   End If
Next
'add all second-level tasks
Dim child As Integer = 0
Dim grandchild As Integer = 0
For Each dr2 As DataRow In DataSet1.DataTable1
   For Each dr2Child As DataRow In BacklogView.DataSet1.DataTable1.Select("ParentTaskID='" & dr2.Item("JobTaskID") & "'")
      If child < TasksHierarchy.Nodes.Count - 1 Then
         TasksHierarchy.Nodes(child).Nodes.Add("Task: " & dr2Child.Item("TaskName") & " - Task Type: " & dr2Child.Item("TaskTypeName") & " - Owner: " & dr2Child.Item("Name"))
      End If
      'add all third-level tasks
      For Each dr2GrandChild As DataRow In DataSet1.DataTable1.Select("ParentTaskID='" & dr2Child.Item("JobTaskID") & "'")
                        TasksHierarchy.Nodes(child).Nodes(grandchild).Nodes.Add("Task: " & dr2GrandChild.Item("TaskName") & " - Task Type: " & dr2GrandChild.Item("TaskTypeName") & " - Owner: " & dr2GrandChild.Item("Name"))
      Next
      grandchild = grandchild + 1
   Next
   child = child + 1
Next


What I have tried:

I have tried making a hierarchical DataGridView to show the dependencies but converting from the online example's C# to VB.Net did not work and the program crashed.
Posted
Updated 25-Apr-16 13:59pm
Comments
Sergey Alexandrovich Kryukov 25-Apr-16 19:40pm    
First, you need to indicate what exactly you mean by "tree view". How about full type name? (It must be System.Windows.Forms.DataGridView.)
Now, how exactly the hierarchy should be represented? Normally, hierarchical control structure is TreeView. How a DataGridView instance should show the hierarchy?
What's wrong with just reading original MSDN documentation?
—SA
Matt Leverich 25-Apr-16 20:41pm    
I've read a lot of MSDN documentation and a lot of it populates a TreeView control with sample data. I can't do that; I have a DataTable I'm looping through to populate the TreeView. And for their order in the hierarchy I only have a TaskID and a ParentTaskID in each row of the DataTable.
Sergey Alexandrovich Kryukov 26-Apr-16 1:11am    
Who are you talking to?

All your wrote is very confusing. TreeView or DataGridView? What is where?

—SA
Matt Leverich 26-Apr-16 9:06am    
@Sergey It is not a DataGridView. It is a TreeView control. You can find it in the Toolbox in Visual Studio. The TreeView is named TasksHierarchy. Sorry for being vague

1 solution

Traversing an arbitrarily deep hierarchical data structure.
Hmmmm....
This looks like a job for: Recursion

The top level processing is essentially the same, except that after it Add()s a node, it calls a recursive function that deals with the direct children. In that function, when a node is Add()ed, the same function is called again to process its children, etc.
(This is a "Depth-first tree traversal".)

==========
Something like this (I'm not a sophisticated VB dev. C# yes.):
VB
' At the appropriate place in the code...
Private Sub AddDescendents(ByVal nodes As TreeNodeCollection, ByVal data As DataTable, ByVal taskID as String)
  For Each dr As DataRow in data.Select("ParentTaskID='" & taskID & "'")
    Dim newNode As TreeNode
    newNode = nodes.Add("Task: " & dr.Item("TaskName") & _
                        " - Task Type: " & dr.Item("TaskTypeName") & _
                        " - Owner: " & dr.Item("Name"))
    AddDescendents(newNode.Nodes, data, dr.Item("JobTaskID"))
  Next
End Sub

' elsewhere ...
TasksHierarchy.BeginUpdate()
TasksHierarchy.Nodes.Clear()
Dim nodes As TreeNodeCollection = TasksHierarchy.Nodes
'populate treeview - add all top-level tasks
For Each dr As DataRow In DataSet1.DataTable1
  If IsDBNull(dr.Item("ParentTaskID")) Then 
    'task is at the top level
    Dim newNode As TreeNode
    newNode = nodes.Add("Task: " & dr.Item("TaskName") & _
                        " - Task Type: " & dr.Item("TaskTypeName") & _
                        " - Owner: " & dr.Item("Name"))
    AddDescendents(newNode.Nodes, DataSet1.DataTable1, dr.Item("JobTaskID"))
  End If
Next
TasksHierarchy.EndUpdate()


(Some of the duplication could be consolidated if I knew VB better...)
 
Share this answer
 
v3
Comments
Matt Leverich 25-Apr-16 20:39pm    
It is just a System.Windows.Forms.TreeView. It's available in the Toolbox on Visual Studio.
Matt Leverich 26-Apr-16 9:24am    
That worked very well! Thank you very much!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900