-
Treeview Duplicates
I'm new to VB.NET (VB6 programmer) and from the various threads i've searched, I have put together the code below to catch duplicates in order to populate a treeview. I'm after the following result:
Audi
- A4
- A5
BMW
- 1 Series
- 3 Series
My data is simply two columns: Audi, A4, Audi, A5, BMW, 1 Series, etc...
Code below:
'-----------------------------------------------------------------
Dim parentrow As DataRow
Dim ParentTable As DataTable
ParentTable = ds.Tables("Makes")
Dim parentnode As TreeNode
Dim lastName As String
lastName = ""
For Each parentrow In ParentTable.Rows
For Each row As DataRow In ParentTable.Rows
If CStr(row("Manufacturer")) <> lastName Then
parentnode = New TreeNode(parentrow.Item(0))
Else
parentnode.Nodes.Add(parentrow.Item(1))
End If
lastName = CStr(row("Manufacturer"))
Next
Next
'----------------------------------------------------------------
Unfortunately the parentnode in the Else part of the code is showing "variable used before assigned". Any help with the above would be appreciated.
Thanks in advance.
-
Re: Treeview Duplicates
[RESOLVED]
Code:
Dim lastName As String
Dim lastModel As String
lastModel = ""
lastName = ""
For Each parentrow In ParentTable.Rows
If CStr(parentrow("Manufacturer")) <> lastName Then
parentnode = New TreeNode(parentrow.Item(0))
TreeView1.Nodes.Add(parentnode)
Else
parentnode.Nodes.Add(parentrow.Item(1))
End If
lastName = CStr(parentrow("Manufacturer"))
Next