Results 1 to 5 of 5

Thread: Loading a CSV Filles data into a TreeView

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Mar 2004
    Posts
    20

    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

  2. #2
    Frenzied Member Mike Hildner's Avatar
    Join Date
    Jul 2002
    Location
    Des Moines, NM
    Posts
    1,690
    Do you have a sample csv file?

  3. #3
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Originally posted by Mike Hildner
    Do you have a sample csv file?
    You can get one anytime from System Tools ---->Event Viewer

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Mar 2004
    Posts
    20

    Example CSV

    Heres an example CSV File in a zip file
    Attached Files Attached Files

  5. #5
    Frenzied Member <ABX's Avatar
    Join Date
    Jul 2002
    Location
    Canada eh...
    Posts
    1,622
    The only way i can see of doing this would be recursion

    Edit: Threwup a little example for you...

    VB Code:
    1. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.         Dim Data As HalcrowNodeCollection = LoadCollection("..\UniclassBasic.csv")
    3.  
    4.         Dim Node As HalcrowTreeNode = SortNodes(Data)
    5.  
    6.         Dim aTreeNode As TreeNode = StartConvert(Node)
    7.  
    8.         TreeView1.Nodes.Add(aTreeNode)
    9.  
    10.     End Sub
    11.  
    12.     Private Function StartConvert(ByVal Node As HalcrowTreeNode) As TreeNode
    13.         Dim rtn As New TreeNode
    14.  
    15.         rtn.Text = Node.Text
    16.         rtn.Tag = Node.Key
    17.  
    18.         ConvertNode(rtn, Node)
    19.  
    20.         Return rtn
    21.  
    22.     End Function
    23.  
    24.     Private Function ConvertNode(ByRef aTreeNode As TreeNode, ByVal aHalcrowNode As HalcrowTreeNode)
    25.  
    26.         For Each n As HalcrowTreeNode In aHalcrowNode.MyNodes
    27.             Dim newNode As New TreeNode
    28.  
    29.             newNode.Text = n.Text
    30.             newNode.Tag = n.Key
    31.             ConvertNode(newNode, n)
    32.             aTreeNode.Nodes.Add(newNode)
    33.         Next
    34.  
    35.     End Function
    36.  
    37.     Private Function LoadCollection(ByVal strFilePath As String) As HalcrowNodeCollection
    38.  
    39.         Dim RawData As New HalcrowNodeCollection
    40.  
    41.         Dim sr As New IO.StreamReader(strFilePath)
    42.  
    43.         Dim strFile() As String = Split(sr.ReadToEnd, vbCrLf)
    44.         sr.Close()
    45.  
    46.         For I As Integer = 0 To strFile.Length - 1
    47.             Dim strTmp() As String = Split(strFile(I), ",")
    48.  
    49.             If strTmp.Length = 3 Then
    50.                 RawData.Add(New HalcrowTreeNode(strTmp(1), strTmp(0), strTmp(2)))
    51.             End If
    52.  
    53.         Next
    54.  
    55.         Return RawData
    56.  
    57.     End Function
    58.  
    59.     Public Function SortNodes(ByVal Nodes As HalcrowNodeCollection) As HalcrowTreeNode
    60.         'Returns The Sorted Main Nodes of the Collection
    61.  
    62.         Dim MainNode As New HalcrowTreeNode("Uniclass", "Uniclass", "") ' main Node has no Parent
    63.  
    64.         GetChildNodes(MainNode, Nodes)
    65.  
    66.         Return MainNode
    67.  
    68.     End Function
    69.  
    70.     Private Function GetChildNodes(ByRef ParentNode As HalcrowTreeNode, ByRef Nodes As HalcrowNodeCollection)
    71.  
    72.         For I As Integer = 0 To Nodes.Count - 1
    73.  
    74.             If ParentNode.Key = Nodes.Item(I).ParentKey Then
    75.                 GetChildNodes(Nodes.Item(I), Nodes)
    76.                 ParentNode.MyNodes.Add(Nodes.Item(I))
    77.             End If
    78.  
    79.         Next
    80.     End Function

    VB Code:
    1. Option Explicit On
    2.  
    3. Public Class HalcrowTreeNode
    4.  
    5.     Private m_strText As String
    6.     Private m_strKey As String
    7.     Private m_strParentKey As String
    8.     Private m_MyNodes As New HalcrowNodeCollection
    9.  
    10.  
    11.     Public Property Text() As String
    12.         Get
    13.             Return m_strText
    14.         End Get
    15.         Set(ByVal Value As String)
    16.             m_strText = Value
    17.         End Set
    18.     End Property
    19.  
    20.     Public Property Key() As String
    21.         Get
    22.             Return m_strKey
    23.         End Get
    24.         Set(ByVal Value As String)
    25.             m_strKey = Value
    26.         End Set
    27.     End Property
    28.  
    29.     Public Property ParentKey() As String
    30.         Get
    31.             Return m_strParentKey
    32.         End Get
    33.         Set(ByVal Value As String)
    34.             m_strParentKey = Value
    35.         End Set
    36.     End Property
    37.  
    38.     Public Sub New(ByVal strText As String, ByVal strKey As String, ByVal strParentKey As String)
    39.         m_strText = strText
    40.         m_strKey = strKey
    41.         m_strParentKey = strParentKey
    42.     End Sub
    43.  
    44.     Public Sub New(ByVal strText As String, ByVal strKey As String, ByVal strParentKey As String, ByVal MyNodes As HalcrowNodeCollection)
    45.  
    46.     End Sub
    47.  
    48.     Public Property MyNodes() As HalcrowNodeCollection
    49.         Get
    50.             Return m_MyNodes
    51.         End Get
    52.         Set(ByVal Value As HalcrowNodeCollection)
    53.             m_MyNodes = Value
    54.         End Set
    55.     End Property
    56. End Class
    57.  
    58. Public Class HalcrowNodeCollection
    59.     Inherits CollectionBase
    60.  
    61.     ' Restricts to HalcrowTreeNode types, items that can be added to the collection.
    62.     Public Sub Add(ByVal aHalcrowTreeNode As HalcrowTreeNode)
    63.         ' Invokes Add method of the List object to add a HalcrowTreeNode.
    64.         List.Add(aHalcrowTreeNode)
    65.     End Sub
    66.  
    67.     ' Visual Basic
    68.     Public Sub Remove(ByVal index As Integer)
    69.         ' Check to see if there is a HalcrowTreeNode at the supplied index.
    70.         If index > Count - 1 Or index < 0 Then
    71.             ' If no HalcrowTreeNode exists, a messagebox is shown and the operation is
    72.             ' cancelled.
    73.             System.Windows.Forms.MessageBox.Show("Index not valid!")
    74.         Else
    75.             ' Invokes the RemoveAt method of the List object.
    76.             List.RemoveAt(index)
    77.         End If
    78.     End Sub
    79.  
    80.     ' This line declares the Item property as ReadOnly, and
    81.     ' declares that it will return a HalcrowTreeNode object.
    82.     Public ReadOnly Property Item(ByVal index As Integer) As HalcrowTreeNode
    83.         Get
    84.             ' The appropriate item is retrieved from the List object and
    85.             ' explicitly cast to the HalcrowTreeNode type, then returned to the
    86.             ' caller.
    87.             Return CType(List.Item(index), HalcrowTreeNode)
    88.         End Get
    89.     End Property
    90.  
    91. 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
  •  



Click Here to Expand Forum to Full Width