|
-
Mar 26th, 2004, 09:29 AM
#1
Thread Starter
Junior Member
Loading a CSV Filles data into a TreeView
I have a CSV file with Hierarchal Data in it which I wish to display in a TreeView.
Each entry in the file has a key which links it to a Parent.
For example one Entry might have the Key D1 so I no that has to go under the parent which has the code D1
I have set up my own NodeKey property which stores the code
The problem is when I find the code which matches the NodeKey property I don't no how to add a new entry under The parent or child heading.
The following code is how I have tried to do it.
'Loops round the nodes within the treeview
If (cNode.NodeKey = nextParent) Then
sString = nextElement & " " & nextDesc
iRecord = cNode.Index
Try
Nextmode = TreeView1.Nodes(iRecord)
Nextmode.Nodes.Add(New HalcrowTreeNode(sString, nextElement))
Does anybody have any ideas PLEASE. It been cause me a great Headache.
Thanks
Daniel
-
Mar 26th, 2004, 01:14 PM
#2
Frenzied Member
Do you have a sample csv file?
-
Mar 26th, 2004, 01:27 PM
#3
Sleep mode
Originally posted by Mike Hildner
Do you have a sample csv file?
You can get one anytime from System Tools ---->Event Viewer
-
Mar 29th, 2004, 03:29 AM
#4
Thread Starter
Junior Member
Example CSV
Heres an example CSV File in a zip file
-
Mar 29th, 2004, 06:47 PM
#5
The only way i can see of doing this would be recursion
Edit: Threwup a little example for you...
VB Code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim Data As HalcrowNodeCollection = LoadCollection("..\UniclassBasic.csv")
Dim Node As HalcrowTreeNode = SortNodes(Data)
Dim aTreeNode As TreeNode = StartConvert(Node)
TreeView1.Nodes.Add(aTreeNode)
End Sub
Private Function StartConvert(ByVal Node As HalcrowTreeNode) As TreeNode
Dim rtn As New TreeNode
rtn.Text = Node.Text
rtn.Tag = Node.Key
ConvertNode(rtn, Node)
Return rtn
End Function
Private Function ConvertNode(ByRef aTreeNode As TreeNode, ByVal aHalcrowNode As HalcrowTreeNode)
For Each n As HalcrowTreeNode In aHalcrowNode.MyNodes
Dim newNode As New TreeNode
newNode.Text = n.Text
newNode.Tag = n.Key
ConvertNode(newNode, n)
aTreeNode.Nodes.Add(newNode)
Next
End Function
Private Function LoadCollection(ByVal strFilePath As String) As HalcrowNodeCollection
Dim RawData As New HalcrowNodeCollection
Dim sr As New IO.StreamReader(strFilePath)
Dim strFile() As String = Split(sr.ReadToEnd, vbCrLf)
sr.Close()
For I As Integer = 0 To strFile.Length - 1
Dim strTmp() As String = Split(strFile(I), ",")
If strTmp.Length = 3 Then
RawData.Add(New HalcrowTreeNode(strTmp(1), strTmp(0), strTmp(2)))
End If
Next
Return RawData
End Function
Public Function SortNodes(ByVal Nodes As HalcrowNodeCollection) As HalcrowTreeNode
'Returns The Sorted Main Nodes of the Collection
Dim MainNode As New HalcrowTreeNode("Uniclass", "Uniclass", "") ' main Node has no Parent
GetChildNodes(MainNode, Nodes)
Return MainNode
End Function
Private Function GetChildNodes(ByRef ParentNode As HalcrowTreeNode, ByRef Nodes As HalcrowNodeCollection)
For I As Integer = 0 To Nodes.Count - 1
If ParentNode.Key = Nodes.Item(I).ParentKey Then
GetChildNodes(Nodes.Item(I), Nodes)
ParentNode.MyNodes.Add(Nodes.Item(I))
End If
Next
End Function
VB Code:
Option Explicit On
Public Class HalcrowTreeNode
Private m_strText As String
Private m_strKey As String
Private m_strParentKey As String
Private m_MyNodes As New HalcrowNodeCollection
Public Property Text() As String
Get
Return m_strText
End Get
Set(ByVal Value As String)
m_strText = Value
End Set
End Property
Public Property Key() As String
Get
Return m_strKey
End Get
Set(ByVal Value As String)
m_strKey = Value
End Set
End Property
Public Property ParentKey() As String
Get
Return m_strParentKey
End Get
Set(ByVal Value As String)
m_strParentKey = Value
End Set
End Property
Public Sub New(ByVal strText As String, ByVal strKey As String, ByVal strParentKey As String)
m_strText = strText
m_strKey = strKey
m_strParentKey = strParentKey
End Sub
Public Sub New(ByVal strText As String, ByVal strKey As String, ByVal strParentKey As String, ByVal MyNodes As HalcrowNodeCollection)
End Sub
Public Property MyNodes() As HalcrowNodeCollection
Get
Return m_MyNodes
End Get
Set(ByVal Value As HalcrowNodeCollection)
m_MyNodes = Value
End Set
End Property
End Class
Public Class HalcrowNodeCollection
Inherits CollectionBase
' Restricts to HalcrowTreeNode types, items that can be added to the collection.
Public Sub Add(ByVal aHalcrowTreeNode As HalcrowTreeNode)
' Invokes Add method of the List object to add a HalcrowTreeNode.
List.Add(aHalcrowTreeNode)
End Sub
' Visual Basic
Public Sub Remove(ByVal index As Integer)
' Check to see if there is a HalcrowTreeNode at the supplied index.
If index > Count - 1 Or index < 0 Then
' If no HalcrowTreeNode exists, a messagebox is shown and the operation is
' cancelled.
System.Windows.Forms.MessageBox.Show("Index not valid!")
Else
' Invokes the RemoveAt method of the List object.
List.RemoveAt(index)
End If
End Sub
' This line declares the Item property as ReadOnly, and
' declares that it will return a HalcrowTreeNode object.
Public ReadOnly Property Item(ByVal index As Integer) As HalcrowTreeNode
Get
' The appropriate item is retrieved from the List object and
' explicitly cast to the HalcrowTreeNode type, then returned to the
' caller.
Return CType(List.Item(index), HalcrowTreeNode)
End Get
End Property
End Class
Last edited by <ABX; Mar 29th, 2004 at 08:03 PM.
Tips:
- Google is your friend! Search before posting!
- Name your thread appropriately... "I Need Help" doesn't cut it!
- Always post your code!!!! We can't read your mind!!! (well, at least most of us!)
- Allways Include the Name and Line of the Exception (if one is occuring!)
- If it is relevant state the version of Visual Studio/.Net Framwork you are using (2002/2003/2005)
If you think I was helpful, rate my post  IRC Contact: Rizon/xous ChakraNET/xous Freenode/xous
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|