473,804 Members | 2,133 Online
Bytes | Software Development & Data Engineering Community
+ 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.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 **
----------------------------------------------------------
http://www.usenet.com
Nov 21 '05 #1
3 2252
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(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" <ad********@hot mail-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.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 **
----------------------------------------------------------
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.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 **
----------------------------------------------------------
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
1480
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
1644
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 so far... I can't run it yet because I don't have the DataTable source... I still want to get some sort of assessment as to whether I am heading in the right direction. Here is the code: ================================ using System;
5
8594
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 treeview nodes through an XML file. As part of each node in the XML, I'm using an attribute that indicates the name of a sub form to load. As part of all these little child forms, the main control is a panel, which I then assign its parent to the...
3
2274
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 call the script from my page, I get "object not found". Temporary Workaround: This only happens when I have "debug=true" in my web.config. If I remove debug=true then then script works fine.
1
2240
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 problems populating the treeview nodes initially, but am somewhat flustered as to how I should go about keeping the treeview control current with the dataset. After the dataset has been refreshed, I can, of course, clear the treeview nodes and...
6
2394
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 them all together. I also have a Login form and a View form, both started by the main form. My goal is to have the Login form pop up as soon as the application is started, and at the same time have the View form start loading data, but stay...
2
4071
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 treeview I would like to use the AFTER_SELECT method to bring up a new form if possible containing a dataset for the record selected. OR as an alternative I would like to bring up a dataset for that record selected on the same form. I have succeeding in...
0
1373
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 itself. The treeview data source is asp XMLDataSource that is transformed by an xlst file. The xml data is generated from SQL database and dynamically bind to the XmlDataSource. Is it possible to use this method to bind data to treeview control?...
1
2032
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 triggered by the SelectedNodeChanged event of the TreeView. I dynamically load user controls into my PlaceHolder depending on what node the user clicks on the TreeView. The user control is loaded into the page and is displayed to the user. These...
0
9715
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9595
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10356
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10099
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9176
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6869
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5675
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3836
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3003
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.