Murugan.com
Murugan Andezuthu Dharmaratnam

  |  HOME   |  BLOG   |  TWITTER   |  ARTICLES   |  8086  |  C++   |  VC++   |  ASP .NET   |  VB .NET   |  JAVA SCRIPT   |  MS SQL   |  MY   |  VIDEOS   |  DOWNLOADS   |  CONTACT ME   |  



ASP .NET How to Populate a TreeView Control With TreeNode s Using recursive algorithm or recursion

Home   ASP.NET   ASP .NET How to Populate a TreeView Control With TreeNode s Using recursive algorithm or recursion          


How to Fill a TreeView Control using recursion or Recursive algorithm. 

The first question I would have is wether the date is coming from an XML file or 
a Database. 

If its from a database Here's a sample TableDesign


ID                             Varchar(8) 
Data                        Varchar(50)
ParentID                 Varchar(8) 

Note that There is a parentID. This parent ID links the child node to its parent. 
Either Way get the data to a DataSet


    Dim oDataSet As New DataSet
    Public Sub PopulateTree(ByVal ParentId As String, ByVal TVNode As TreeNode)
        Dim oDataView As New DataView(oDataSet.Tables(0), "ParentID='" & ParentId & "'", 
        "DATA", DataViewRowState.OriginalRows)
        Dim oDataRow As DataRowView
        For Each oDataRow In oDataView
            Dim oTreeNode As New TreeNode(oDataRow("DATA"))
            Dim oComboBox As New ComboBox
            If TVNode Is Nothing Then
            Else
                TVNode.ChildNodes.Add(oTreeNode)
                ' For Winform replace the above line with TVNode.Nodes.Add(oTreeNode)
                PopulateTree(oDataRow("ID"), oTreeNode)
            End If
        Next
    End Sub

This is how U call the above function 
PopulateTree(0, tvPost.TopNode)
tvPost - its the name of TreeView TreeViewControl I had Dragged & Dropped From tool bar






  |  HOME   |  BLOG   |  TWITTER   |  ARTICLES   |  8086  |  C++   |  VC++   |  ASP .NET   |  VB .NET   |  JAVA SCRIPT   |  MS SQL   |  MY   |  VIDEOS   |  DOWNLOADS   |  CONTACT ME   |  

Copyright 2009 @ Murugan Andezuthu Dharmaratnam