473,473 Members | 2,255 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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.OleDbConnection = New
Data.OleDb.OleDbConnection
("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
OpenFileDialog1.FileName & ";Persist Security
Info=False;")
Dim DR As Data.OleDb.OleDbDataReader
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.OleDbCommand = New
Data.OleDb.OleDbCommand
(sqlNames, Conn)

' Clear Treeview Nodes
TreeView1.Nodes.Clear()

Try
Conn.Open()
DR = Cmd.ExecuteReader

If DR.HasRows = False Then
TreeView1.Nodes.Add("No Users on file")
TreeView1.ForeColor = Color.Red
NoUsers = True 'Boolean to tell other objects that
there are no users.
Else
TreeView1.ForeColor = 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(currentAlpha))

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

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

System.Windows.Forms.Application.DoEvents()
TreeView1.Nodes(indx - 65).Nodes.Add(New
TreeNode(sContactName))

End If

Next
End While
DR.Close()
Conn.Close()
End If
Catch LX As Exception
MsgBox(LX.Message, MsgBoxStyle.Exclamation, "")
End Try
TreeView1.ExpandAll()
*---------------------------------*
Posted at: http://www.GroupSrv.com
*---------------------------------*

Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Nov 21 '05 #1
3 2236
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

Nov 21 '05 #2
Hi,

Maybe this will help.

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Dim conn As SqlConnection

Dim daCustomers As SqlDataAdapter

Dim daOrders As SqlDataAdapter

Dim strConn As String

strConn = "Data Source = " + SystemInformation.ComputerName

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

strConn += "Integrated Security = SSPI;"

conn = New SqlConnection(strConn)

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

daCustomers.Fill(ds, "Clients")

dvCustomers = New DataView(ds.Tables("Clients"))

AddNodes()

trvNorthWind.Sorted = 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(strName)

Dim snCountry As TreeNode

Dim snCity As TreeNode

snCity = New TreeNode(drCustomer.Item("City"))

snCountry = New TreeNode(drCustomer.Item("Country"))

snCountry.Nodes.Add(snCity)

n.Nodes.Add(snCountry)

trvNorthWind.Nodes.Add(n)

Next drCustomer

..EndUpdate()

End With

End Sub

Ken
-------------------------
"Troy" <ad********@hotmail-dot-com.no-spam.invalid> wrote in message
news:42********@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.OleDbConnection = New
Data.OleDb.OleDbConnection
("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
OpenFileDialog1.FileName & ";Persist Security
Info=False;")
Dim DR As Data.OleDb.OleDbDataReader
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.OleDbCommand = New
Data.OleDb.OleDbCommand
(sqlNames, Conn)

' Clear Treeview Nodes
TreeView1.Nodes.Clear()

Try
Conn.Open()
DR = Cmd.ExecuteReader

If DR.HasRows = False Then
TreeView1.Nodes.Add("No Users on file")
TreeView1.ForeColor = Color.Red
NoUsers = True 'Boolean to tell other objects that
there are no users.
Else
TreeView1.ForeColor = 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(currentAlpha))

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

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

System.Windows.Forms.Application.DoEvents()
TreeView1.Nodes(indx - 65).Nodes.Add(New
TreeNode(sContactName))

End If

Next
End While
DR.Close()
Conn.Close()
End If
Catch LX As Exception
MsgBox(LX.Message, MsgBoxStyle.Exclamation, "")
End Try
TreeView1.ExpandAll()
*---------------------------------*
Posted at: http://www.GroupSrv.com
*---------------------------------*

Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Nov 21 '05 #3
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.OleDbConnection = New
Data.OleDb.OleDbConnection("Provider=Microsoft.Jet .OLEDB.4.0;Data
Source=" & _
OpenFileDialog1.FileName & ";Persist Security
Info=False;")
Dim DR As Data.OleDb.OleDbDataReader
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.OleDbCommand = New
Data.OleDb.OleDbCommand(sqlNames, Conn)

' Clear Treeview Nodes
TreeView1.Nodes.Clear()
Try

Conn.Open()
DR = Cmd.ExecuteReader
If DR.HasRows = False Then
TreeView1.Nodes.Add("No Users on file")
TreeView1.ForeColor = Color.Red
NoUsers = True 'Boolean to tell other objects that
there are no users.
Else
TreeView1.ForeColor = 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(currentAlpha))
While DR.Read()
' Add a child TreeNode for each Customer
object in the current Alpha Character.

If
UCase(Microsoft.VisualBasic.Left(DR("LastName1"), 1)) = currentAlpha
Then

sContactName = DR("Lastname1") & ", "
& DR("FirstName1") & " " & DR("MiddleInitial1") &
"."
System.Windows.Forms.Application.DoEvents()

TreeView1.Nodes(indx - 65).Nodes.Add(New
TreeNode(sContactName, 1, 1))
End If
End While
DR.Restart()
Next
DR.Close()
Conn.Close()
End If

Catch LX As Exception
MsgBox(LX.Message, MsgBoxStyle.Exclamation, "")
End Try
TreeView1.ExpandAll()

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 **
----------------------------------------------------------
http://www.usenet.com
Nov 21 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: SeVeN | last post by:
Hello All, I am having a problem loading nodes and child nodes into a treeview from an access database. My Db fields are as follows : ID NodeName ParentID
0
by: Henry | last post by:
I am trying to create a TreeView control that works with an ADO Dataset DataTable or the new BindingSource stuff in .NET 2.0 to build a Treeview that is populated. This is what I came up with...
5
by: marfi95 | last post by:
I have a form that has a left and right panel. In the left panel is a treeview. The right panel I want to change dynamically based on the type of node selected. What I'm doing is loading the...
3
by: GroupReader | last post by:
I posted a similar question earlier and got lots of good feedback, but now I have more information: Problem: I have javascript in a user control that is not "loading" properly. When I try to...
1
by: echuck66 | last post by:
Hi, I have a Winforms 2.0 project that I'm working on that involves populating a treeview control from data contained in a fairly large dataset that has to be refreshed periodically. I have no...
6
by: A.Weinman | last post by:
Hello all, I have an application that has multiple forms, only 3 of which matter for this issue. There is a main form that starts invisible and does nothing more than start other forms and link...
2
by: dav61000 | last post by:
I am new to VB.net so I am not sure if there is a good way to do this or not but here is my problem. I have created a form with a TreeView control on it. When the user selects a node from the...
0
by: =?Utf-8?B?SmVmZiBHYW8=?= | last post by:
Hi, I am working on an asp.net web application that uses treeview control to display hierarchical data from an xml data source. When I changed the xml data source the treeview doesn’t update...
1
by: Christian Resma Helle | last post by:
Hey guys, I'm working on an AJAX Enabled ASP.NET Web application. I have a TreeView web control and an PlaceHolder web control. My PlaceHolder is inside an UpdatePanel and AsyncPostBacks are...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.