Results 1 to 2 of 2

Thread: need help with treeview PLEASE!!

  1. #1

    Thread Starter
    Lively Member spdracr's Avatar
    Join Date
    Oct 2003
    Location
    Kansas
    Posts
    115

    need help with treeview PLEASE!!

    ok, i have a collection that i need to populate a treeview with. i can get the root nodes in and child nodes under that, but i also need to get some child nodes under the already child nodes.

    how can i do this...

    also, does anybody have a good way of sending a collection to a treeview. all examples i have seen use an array. using an array seems to work a bit easier seeing how you have indexes to work with and you don't with a collection.

    but hey, maybe i'm just screwed in the head and don't know what i'm talking about... i've only been using vb.net for 2 days now.

    thanks

  2. #2
    Hyperactive Member
    Join Date
    Mar 2002
    Location
    Dublin (Ireland)
    Posts
    304
    Yes had the same problem intially, then found somegeneric code which showed how to polate a treeview and indent as much as you like.

    I'm going to ppost my own code here. For our appplication, which inhererit the treeview control to add additonal properties, so I've included that class as well:

    I did post this previously using another exampple, hopefully between the two examples you will work it out.

    Fist the inherited node class:

    Public Class TfrNode
    Inherits TreeNode

    Public Name As String
    Public Landlord As Integer
    Public Proprty As Integer
    Public Unit As Integer
    Public Tenancy As Integer


    Sub New(ByVal Desc As String, ByVal Landlord1 As Integer, ByVal Proprty1 As Integer, ByVal Unit1 As Integer, ByVal Tenancy1 As Integer)
    MyBase.New()
    Name = Desc
    Landlord = Landlord1
    Proprty = Proprty1
    Unit = Unit1
    Tenancy = Tenancy1
    Me.Text = Name
    End Sub

    End Class

    Now the code which reads from the datasets and popluates three tree view, this indent to a level of 3,but as you can see the code is structured toallow more indentation if required.

    Private Sub Populatetree()
    Dim OledbStr As String = "Select Landlord,Name,Adr1,Adr2 from Landlords where (DateRemoved Is Null)"

    Dim Cmd1 As OledbCommand = New OledbCommand(OledbStr, Cn1)
    Cn1.Open()
    Cn2.Open()
    Cn3.Open()
    Cn4.Open()
    Dim dt1 As OledbDataReader = Cmd1.ExecuteReader()
    TreeView1.BeginUpdate()
    TreeView1.Nodes.Clear()
    Dim AllNode As New TfrNode("Landlords", 0, 0, 0, 0)
    TreeView1.Nodes.Add(AllNode)
    ' Build Landlords list
    Dim BalTfr As Boolean
    While dt1.Read
    BalTfr = LandlordBal(dt1("Landlord"))

    If BalTfr Then
    Dim LanNode As New TfrNode(Trim(dt1("Name")) & " " & Trim(dt1("Adr1")) & "," & Trim(dt1("Adr2")), dt1("Landlord"), 0, 0, 0)
    AllNode.Nodes.Add(LanNode)
    ' buid landlord properties list
    OledbStr = "Select Proprty,Adr1,Adr2,Locality from Properties where (DateRemoved Is Null)and Landlord = " & CStr(dt1("Landlord"))
    Dim Cmd2 As OledbCommand = New OledbCommand(OledbStr, Cn2)
    Dim dt2 As OledbDataReader = Cmd2.ExecuteReader()
    While dt2.Read
    BalTfr = PropBal(dt2("Proprty"))


    If BalTfr Then
    Dim PropNode As New TfrNode((Trim(dt2("Adr1")) & "," & Trim(dt2("Adr2")) & "," & Trim(dt2("Locality"))), dt1("Landlord"), dt2("Proprty"), 0, 0)
    LanNode.Nodes.Add(PropNode)
    ' build units list for this Proprty
    OledbStr = "Select Proprty,Unit,PropertyAdr from PropertyUnits where (DateRemoved Is Null) and Proprty = " & CStr(dt2("Proprty"))
    Dim Cmd3 As OledbCommand = New OledbCommand(OledbStr, Cn3)
    Dim dt3 As OledbDataReader = Cmd3.ExecuteReader()

    While dt3.Read
    BalTfr = UnitBal(dt2("Proprty"), dt3("Unit"))

    If BalTfr Then
    OledbStr = "Select Tenancy,Proprty,Unit,LeadTenant from Tenancies where (DateRemoved Is Null) and Proprty = " & CStr(dt2("Proprty")) & " and Unit = " & CStr(dt3("Unit"))
    Dim Cmd4 As OledbCommand = New OledbCommand(OledbStr, Cn4)
    da4.SelectCommand = Cmd4
    ds4.Clear()
    Dim found As Integer = da4.Fill(ds4, "Tenancies")
    Dim Details As String
    Dim tenancy As Integer
    If found > 0 Then
    Dr4 = ds4.Tables.Item("Tenancies").Rows(0)
    tenancy = Dr4("Tenancy")
    Details = Trim(dt3("PropertyAdr")) & "," & Trim(Dr4("LeadTenant"))
    Else
    tenancy = 0
    Details = dt3("PropertyAdr")
    End If

    Dim UnitNode As New TfrNode(Details, dt1("Landlord"), dt2("Proprty"), dt3("Unit"), tenancy)
    PropNode.Nodes.Add(UnitNode)

    Cmd4 = Nothing
    End If

    End While
    dt3.Close()
    Cmd3 = Nothing

    End If
    End While
    dt2.Close()
    Cmd2 = Nothing
    End If
    End While
    TreeView1.EndUpdate()
    Cmd1 = Nothing
    Cn1.Close()
    Cn2.Close()
    Cn3.Close()
    Cn4.Close()
    End Sub

    Don't worry about the specifics, it is simply a question of starting at the root node, then adding all levels between the next branch in the tree before moving onto the next branch at the same level.

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