Loading Data into Treeview Control?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Troy

    Loading Data into Treeview Control?

    I've almost got this the way I want it. I'm loading my customer names
    into a treeview control. My problem is I'm repeating my root nodes. I
    know it's something to do with my loop structure but My eyes are
    crossing from it can someone help me get this organised lol. here's
    my code.

    Private Sub updateTree()
    Dim Conn As Data.OleDb.OleD bConnection = New
    Data.OleDb.OleD bConnection
    ("Provider=Micr osoft.Jet.OLEDB .4.0;Data Source=" & _
    OpenFileDialog1 .FileName & ";Persist Security
    Info=False;")
    Dim DR As Data.OleDb.OleD bDataReader
    Dim indx As Short
    Dim NoUsers As Boolean
    Dim sqlNames As String
    Dim currentAlpha As String
    Dim sContactName As String

    sqlNames = "SELECT ContactID, LastName1, FirstName1,
    MiddleInitial1 "
    sqlNames = sqlNames & "FROM Contact ORDER BY"
    sqlNames = sqlNames & " LastName1, FirstName1,
    MiddleInitial1 "
    Dim Cmd As Data.OleDb.OleD bCommand = New
    Data.OleDb.OleD bCommand
    (sqlNames, Conn)

    ' Clear Treeview Nodes
    TreeView1.Nodes .Clear()

    Try
    Conn.Open()
    DR = Cmd.ExecuteRead er

    If DR.HasRows = False Then
    TreeView1.Nodes .Add("No Users on file")
    TreeView1.ForeC olor = Color.Red
    NoUsers = True 'Boolean to tell other objects that
    there are no users.
    Else
    TreeView1.ForeC olor = Color.Black
    NoUsers = False 'Boolean to tell other objects that
    there are users. End If

    While DR.Read

    For indx = Asc("A") To Asc("Z")
    currentAlpha = Chr(indx)
    TreeView1.Nodes .Add(New
    TreeNode(curren tAlpha))

    ' Add a child TreeNode for each Customer
    object in the current Alpha
    Character.
    If
    UCase(Microsoft .VisualBasic.Le ft(DR("LastName 1"), 1)) = currentAlpha

    Then
    sContactName = DR("Lastname1" ) & ", "
    & DR("FirstName1" ) & " " &
    DR("MiddleIniti al1") & "."

    System.Windows. Forms.Applicati on.DoEvents()
    TreeView1.Nodes (indx - 65).Nodes.Add(N ew
    TreeNode(sConta ctName))

    End If

    Next
    End While
    DR.Close()
    Conn.Close()
    End If
    Catch LX As Exception
    MsgBox(LX.Messa ge, MsgBoxStyle.Exc lamation, "")
    End Try
    TreeView1.Expan dAll()
    *---------------------------------*
    Posted at: http://www.GroupSrv.com
    *---------------------------------*

    Posted Via Usenet.com Premium Usenet Newsgroup Services
    ----------------------------------------------------------
    ** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
    ----------------------------------------------------------
    Best Usenet Service Providers 2025 ranked by Newsgroup Access Newsservers, Usenet Search, Features & Free Trial. Add VPN for privacy.

  • Larry Lard

    #2
    Re: Loading Data into Treeview Control?

    This is what your code is doing at the moment:

    - For each row in the data set
    - - create 26 root nodes, one for each letter
    - - put this row under the appropriate letter node

    Which is why you are getting (I assume) repeating root nodes. What I
    recommend you do is this:

    - have 'currentLetter' and 'currentNode' variables, initially set to ""
    and Nothing
    - for each row in the data set
    - - if this is a new letter (that is, if left(name,1) <> currentletter)
    then:
    - - - set currentLetter to left(name, 1)
    - - - create a new root node with this letter as its text
    - - end if
    - - put this row in a new node under currentNode
    - next

    Note that this method will note create 'empty' root letter notes, ie if
    there are no names beginning with X, there will be no X node, etc

    How's that work for you?

    --
    Larry Lard
    Replies to group please

    Comment

    • Ken Tucker [MVP]

      #3
      Re: Loading Data into Treeview Control?

      Hi,

      Maybe this will help.

      Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As
      System.EventArg s) Handles MyBase.Load

      Dim conn As SqlConnection

      Dim daCustomers As SqlDataAdapter

      Dim daOrders As SqlDataAdapter

      Dim strConn As String

      strConn = "Data Source = " + SystemInformati on.ComputerName

      strConn += "\VSdotNet; Initial Catalog = NorthWind;"

      strConn += "Integrated Security = SSPI;"

      conn = New SqlConnection(s trConn)

      daCustomers = New SqlDataAdapter( "Select * from Employees order by LastName,
      FirstName", conn)

      daCustomers.Fil l(ds, "Clients")

      dvCustomers = New DataView(ds.Tab les("Clients"))

      AddNodes()

      trvNorthWind.So rted = True

      End Sub

      Private Sub AddNodes()

      Dim drCustomer As DataRowView

      Dim root As System.Windows. Forms.TreeNode

      Dim strName As String

      With trvNorthWind

      ..BeginUpdate()

      ..Nodes.Clear()

      For Each drCustomer In dvCustomers

      strName = drCustomer.Item ("FirstName" ) & " " & drCustomer.Item ("LastName")

      Dim n As TreeNode

      n = New TreeNode(strNam e)

      Dim snCountry As TreeNode

      Dim snCity As TreeNode

      snCity = New TreeNode(drCust omer.Item("City "))

      snCountry = New TreeNode(drCust omer.Item("Coun try"))

      snCountry.Nodes .Add(snCity)

      n.Nodes.Add(snC ountry)

      trvNorthWind.No des.Add(n)

      Next drCustomer

      ..EndUpdate()

      End With

      End Sub



      Ken
      -------------------------
      "Troy" <adamtdavis@hot mail-dot-com.no-spam.invalid> wrote in message
      news:4206d85a_4 @127.0.0.1...
      I've almost got this the way I want it. I'm loading my customer names
      into a treeview control. My problem is I'm repeating my root nodes. I
      know it's something to do with my loop structure but My eyes are
      crossing from it can someone help me get this organised lol. here's
      my code.

      Private Sub updateTree()
      Dim Conn As Data.OleDb.OleD bConnection = New
      Data.OleDb.OleD bConnection
      ("Provider=Micr osoft.Jet.OLEDB .4.0;Data Source=" & _
      OpenFileDialog1 .FileName & ";Persist Security
      Info=False;")
      Dim DR As Data.OleDb.OleD bDataReader
      Dim indx As Short
      Dim NoUsers As Boolean
      Dim sqlNames As String
      Dim currentAlpha As String
      Dim sContactName As String

      sqlNames = "SELECT ContactID, LastName1, FirstName1,
      MiddleInitial1 "
      sqlNames = sqlNames & "FROM Contact ORDER BY"
      sqlNames = sqlNames & " LastName1, FirstName1,
      MiddleInitial1 "
      Dim Cmd As Data.OleDb.OleD bCommand = New
      Data.OleDb.OleD bCommand
      (sqlNames, Conn)

      ' Clear Treeview Nodes
      TreeView1.Nodes .Clear()

      Try
      Conn.Open()
      DR = Cmd.ExecuteRead er

      If DR.HasRows = False Then
      TreeView1.Nodes .Add("No Users on file")
      TreeView1.ForeC olor = Color.Red
      NoUsers = True 'Boolean to tell other objects that
      there are no users.
      Else
      TreeView1.ForeC olor = Color.Black
      NoUsers = False 'Boolean to tell other objects that
      there are users. End If

      While DR.Read

      For indx = Asc("A") To Asc("Z")
      currentAlpha = Chr(indx)
      TreeView1.Nodes .Add(New
      TreeNode(curren tAlpha))

      ' Add a child TreeNode for each Customer
      object in the current Alpha
      Character.
      If
      UCase(Microsoft .VisualBasic.Le ft(DR("LastName 1"), 1)) = currentAlpha

      Then
      sContactName = DR("Lastname1" ) & ", "
      & DR("FirstName1" ) & " " &
      DR("MiddleIniti al1") & "."

      System.Windows. Forms.Applicati on.DoEvents()
      TreeView1.Nodes (indx - 65).Nodes.Add(N ew
      TreeNode(sConta ctName))

      End If

      Next
      End While
      DR.Close()
      Conn.Close()
      End If
      Catch LX As Exception
      MsgBox(LX.Messa ge, MsgBoxStyle.Exc lamation, "")
      End Try
      TreeView1.Expan dAll()
      *---------------------------------*
      Posted at: http://www.GroupSrv.com
      *---------------------------------*

      Posted Via Usenet.com Premium Usenet Newsgroup Services
      ----------------------------------------------------------
      ** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
      ----------------------------------------------------------
      Best Usenet Service Providers 2025 ranked by Newsgroup Access Newsservers, Usenet Search, Features & Free Trial. Add VPN for privacy.



      Comment

      • Troy

        #4
        re:Loading Data into Treeview Control?

        LOL thanks guys I got it to work but here's the kicker.

        Works perfect but using Visual Basic .NET 2005 Beta and according to
        the tooltip for my fix the command is being taken out after beta is
        over and no longer used so back to square one on this.

        Here's the code that works though:
        Private Sub updateTree()
        Dim Conn As Data.OleDb.OleD bConnection = New
        Data.OleDb.OleD bConnection("Pr ovider=Microsof t.Jet.OLEDB.4.0 ;Data
        Source=" & _
        OpenFileDialog1 .FileName & ";Persist Security
        Info=False;")
        Dim DR As Data.OleDb.OleD bDataReader
        Dim indx As Short
        Dim NoUsers As Boolean
        Dim sqlNames As String
        Dim currentAlpha As String
        Dim sContactName As String
        sqlNames = "SELECT ContactID, LastName1, FirstName1,
        MiddleInitial1 "
        sqlNames = sqlNames & "FROM Contact ORDER BY"
        sqlNames = sqlNames & " LastName1, FirstName1,
        MiddleInitial1 "
        Dim Cmd As Data.OleDb.OleD bCommand = New
        Data.OleDb.OleD bCommand(sqlNam es, Conn)

        ' Clear Treeview Nodes
        TreeView1.Nodes .Clear()


        Try

        Conn.Open()
        DR = Cmd.ExecuteRead er
        If DR.HasRows = False Then
        TreeView1.Nodes .Add("No Users on file")
        TreeView1.ForeC olor = Color.Red
        NoUsers = True 'Boolean to tell other objects that
        there are no users.
        Else
        TreeView1.ForeC olor = Color.Black
        NoUsers = False 'Boolean to tell other objects that
        there are users.

        For indx = Asc("A") To Asc("Z")
        currentAlpha = Chr(indx)
        TreeView1.Nodes .Add(New TreeNode(curren tAlpha))


        While DR.Read()
        ' Add a child TreeNode for each Customer
        object in the current Alpha Character.

        If
        UCase(Microsoft .VisualBasic.Le ft(DR("LastName 1"), 1)) = currentAlpha
        Then

        sContactName = DR("Lastname1" ) & ", "
        & DR("FirstName1" ) & " " & DR("MiddleIniti al1") &
        "."


        System.Windows. Forms.Applicati on.DoEvents()

        TreeView1.Nodes (indx - 65).Nodes.Add(N ew
        TreeNode(sConta ctName, 1, 1))


        End If
        End While
        DR.Restart()
        Next
        DR.Close()
        Conn.Close()
        End If





        Catch LX As Exception
        MsgBox(LX.Messa ge, MsgBoxStyle.Exc lamation, "")
        End Try
        TreeView1.Expan dAll()

        The command in [b:1553f9a06b]BOLD[/b:1553f9a06b] is what is being
        taken out after Beta is over.

        Anyone else know of a way to reset the stack pointer to the top of the
        DB again once it had gone through the database once?

        or

        Offer another approach using the OleDBConnection method?
        *---------------------------------*
        Posted at: http://www.GroupSrv.com
        *---------------------------------*

        Posted Via Usenet.com Premium Usenet Newsgroup Services
        ----------------------------------------------------------
        ** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
        ----------------------------------------------------------
        Best Usenet Service Providers 2025 ranked by Newsgroup Access Newsservers, Usenet Search, Features & Free Trial. Add VPN for privacy.

        Comment

        Working...