?
Solved

Treeviewitem and for loop

Posted on 2014-11-05
4
Medium Priority
?
136 Views
Last Modified: 2014-11-07
I have a legacy code I'm maintaining that uses a for loop to bind data rows from a dataset into a tree view for wpf. A request came in to add a root node to the existing 2 level tree view. I'm not familiar on how to assign child nodes in treeview control and a few attempts so far has been unsuccessful.  

For Each iRow In iDataSet.Tables("Table1").Rows
                If iRow("TABNAME") <> PreviousTab Then
                    Dim oTreeNodeTabName As New TreeViewItem
                    oTreeNodeTabName.Header = iRow("TABNAME")
                    oTreeNodeTabName.Tag = Nothing
                                        
                    _TreeView.Items.Add(oTreeNodeTabName)
                End If

                If iRow("TYPEDESC") <> PreviousDesc Then
                    Dim oTreeNodeHeader As New TreeViewItem
                    oTreeNodeHeader.Header = iRow("TYPEDESC")
                    oTreeNodeHeader.Tag = Nothing
                    count += 1                    
                    _TreeView.items.Add(oTreeNodeHeader)
                End If

                Dim oTreeNode As New TreeViewItem
                oTreeNode.Header = iRow("EVENTDATE") & "   " & iRow("TYPEDESC")
                oTreeNode.Tag = iRow("VIEWPARMS")
                                
                Dim tvItem As TreeViewItem = _TreeView.Items(count)
                tvItem.Items.Add(oTreeNode)

Open in new window


The data in the table should look like below:

TABNAME     TYPEDESC     EVENTDATE     VIEWPARMS
A                     ABC                MM/DD/YYYY  1234567
A                     ABC                MM/DD/YYYY  2164546
A                     DEF                MM/DD/YYYY  1546546
B                     GHI                MM/DD/YYYY  6548484

I want to use for loop to add a root node for TABNAME so the treeview will have 3 tiers instead of 2:

TABNAME
       |_       TYPEDESC
                          |_    EVENTDATE TYPEDESC
0
Comment
Question by:lo_oscar
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 40425727
it should look like this, assuming the data comes in ordered by tabname and typedes:

string PreviousTab = string.Empty
string PreviousType = string.Empty

Dim oTreeNodeTab As TreeViewItem
Dim oTreeNodeType As TreeViewItem
Dim oTreeNodeChild As TreeViewItem

For Each iRow In iDataSet.Tables("Table1").Rows
       If iRow("TABNAME") <> PreviousTab Then
                oTreeNodeTab = new  TreeViewItem()
                    oTreeNodeTab .Header = iRow("TABNAME")
                    oTreeNodeTab .Tag = Nothing
                                       
                    _TreeView.Items.Add(oTreeNodeTab )
                   PreviousTab=  iRow("TABNAME")
                End If

                If iRow("TYPEDESC") <> PreviousDesc Then
                    oTreeNodeType  = New TreeViewItem
                    oTreeNodeType .Header = iRow("TYPEDESC")
                    oTreeNodeType .Tag = Nothing
                    _oTreeNodeTab .items.Add(oTreeNodeType )
                   PreviousDesc =  iRow("TYPEDESC")
                End If

                oTreeNodeChild =  New TreeViewItem
                oTreeNodeChild .Header = iRow("EVENTDATE") & "   " & iRow("TYPEDESC")
                oTreeNodeChild .Tag = iRow("VIEWPARMS")
                                
                oTreeNodeType .Items.Add(oTreeNodeChild )
end for                                  

Open in new window

0
 

Author Comment

by:lo_oscar
ID: 40426780
Thanks. I have try this too but are getting an error

"Element already has a logical parent. It must be detached from the old parent before it is attached to the new one."

This happens when the second oTreeNodeChild is added while I run debug.
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 total points
ID: 40427810
I am quite sure you did not use the code as meant.

in your code you had this:
Dim oTreeNodeChild As New TreeViewItem

but it must be (before the loop):
Dim oTreeNodeChild As TreeViewItem
and inside the loop:
oTreeNodeChild =  New TreeViewItem

the issue is that if you don't create a new object, it will
0
 

Author Comment

by:lo_oscar
ID: 40428494
Thank you for the explanation. It was spot on.
0

Featured Post

Give Your Engineering Team a Productivity Boost

Learn why container technology is so powerful and how it can provide your team with productivity gains and other benefits.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question