473,407 Members | 2,312 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Data from one table to populate a VB TreeView

KodeKrazy
I have a table with the following columns, ID (A unique value) Style#, Color, Size (There a more columns than that, but those are the ones I will need for this exercise.)

There is a row for each Style#, in every Color, and every Size. (e.g.:
Rec1) Style A, White, Small
Rec2) Style A, White, Med
Rec3) Style A, White, Large
Rec4) Style A, Black, Small
Rec5) Style A, Black, Med
Rec6) Style B, White, Small
Rec7) Style B, Black, Large

etc...)

I want to populate the TreeView by Style, then Color, then Size. As evidenced by the sample data above, there can be sizes, colors available in one style, that are not available in another, etc.

I have thus far been able to extract data to populate the first two nodes on the control, but breaking out the sizes by color, is proving to be slightly beyond the reach of my present abilities.

I know I will probably need a Group By clause, but whenever I try to nest SELECT statements with Group By, I get an error!

Anyone have a suggestion? (I also posted this question in the SQL Server, MSSQL forum.)
Mar 17 '08 #1
5 3762
QVeen72
1,445 Expert 1GB
Hi,

No, you dont need a "Group By".
Its easy if done in 2 steps.. Follow this Logic:

First Query Distinct Styles From Database:
Select Distinct Style From MyTable.
Loop through recordset and Populate TreeView StyleName (as Text and Key) as Parent Nodes/Root Nodes

Now Query Rest of the data
Select Distinct Style, Colour, Size From MyTable Order By Style, Colour,Size.
Loop through this recordset and Populate TreeView Colour and Size (as Text and Key) as Child for the Parent = Style

Regards
Veena
Mar 18 '08 #2
Hi,

No, you dont need a "Group By".
Its easy if done in 2 steps.. Follow this Logic:

First Query Distinct Styles From Database:
Select Distinct Style From MyTable.
Loop through recordset and Populate TreeView StyleName (as Text and Key) as Parent Nodes/Root Nodes

Now Query Rest of the data
Select Distinct Style, Colour, Size From MyTable Order By Style, Colour,Size.
Loop through this recordset and Populate TreeView Colour and Size (as Text and Key) as Child for the Parent = Style

Regards
Veena
This is what the Treeview control looks like so far:

+ Style A
- Black
- White
+ Style B
- Black
- Red

But there are no records being returned for Size (childrow2 = Nothing)

Here is my code so far:

Expand|Select|Wrap|Line Numbers
  1.  
  2. Dim DSProducts As DataSet
  3.         Dim CNproducts As New SqlClient.SqlConnection("DATA SOURCE=.\SQLDBNAME; INITIAL CATALOG=dbTableName; Integrated Security = True") 
  4.         Dim DAItems As New SqlClient.SqlDataAdapter("SELECT DISTINCT Style, Description FROM Items WHERE Catalog = 4", CNproducts)
  5.         Dim DAColors As New SqlClient.SqlDataAdapter("SELECT DISTINCT Color, Style FROM Items WHERE Catalog = 4", CNproducts)
  6.         Dim DASizes As New SqlClient.SqlDataAdapter("SELECT DISTINCT Size, SizeOrder, Style, Color from Items WHERE Catalog = 4", CNproducts)
  7.  
  8.         DSProducts = New DataSet()
  9.         CNproducts.Open()
  10.         DAItems.Fill(DSProducts, "dtItems")
  11.         DAColors.Fill(DSProducts, "dtColors")
  12.         DASizes.Fill(DSProducts, "dtSize")
  13.  
  14.         'Create a data relation object to facilitate the relationship between the Colors and Sizes data.
  15.         DSProducts.Relations.Add("ItemToColor",DSProducts.Tables("dtItems").Columns("Style"),DSProducts.Tables("dtColors").Columns("Style"))
  16.  
  17. ' Tried both of these.Neither relation returns any records.
  18. DSProducts.Relations.Add("ItemToSize",DSProducts.Tables("dtItems").Columns("Style"),DSProducts.Tables("dtSize").Columns("Style"))       
  19. 'DSProducts.Relations.Add("ColorToSize",DSProducts.Tables("dtColors").Columns("Color"),DSProducts.Tables("dtSize").Columns("Color"))
  20.  
  21.         TreeView1.Nodes.Clear()
  22.         Dim parentrow As DataRow
  23.         Dim ParentTable As DataTable
  24.         ParentTable = DSProducts.Tables("dtItems")
  25.  
  26.         For Each parentrow In ParentTable.Rows
  27.  
  28.             Dim parentnode As TreeNode
  29.             parentnode = New TreeNode(parentrow.Item(0) & " " & parentrow.Item(1))
  30.             TreeView1.Nodes.Add(parentnode)
  31.             '   populate child
  32.  
  33.             Dim childrow As DataRow
  34.             Dim childnode As TreeNode
  35.             childnode = New TreeNode()
  36.             For Each childrow In parentrow.GetChildRows("ItemToColor")
  37.                 childnode = parentnode.Nodes.Add(childrow(0))
  38.                 childnode.Tag = childrow("Color")
  39.  
  40.                 '    populate child2
  41.  
  42.                 Dim childrow2 As DataRow
  43.  
  44.                 Dim childnode2 As TreeNode
  45.                 childnode2 = New TreeNode()
  46.                 For Each childrow2 In childrow.GetChildRows("ItemToSize")
  47.                     childnode2 = childnode.Nodes.Add(childrow2(0))
  48.                     childnode2.Tag = childrow("Size")
  49.  
  50.                 Next childrow2
  51.  
  52.             Next childrow
  53.  
  54.         Next parentrow
  55.  
  56.  
I need to know what I am doing wrong that the last group (Size) is not relating back to the Item & Color.

Thanks for your help!

KK
Mar 18 '08 #3
QVeen72
1,445 Expert 1GB
Hi,

Why you have commented 18th line...?
remove the comment, and 45th line change it to :

For Each childrow2 In childrow.GetChildRows("ColorToSize")

Regards
Veena
Mar 19 '08 #4
The reason it is commented out relates to the comment on line 16. I had tried both of those relations. (lines 17 & 18) However, when I uncomment line 18 (and change line 45, per your instructions), I get the error "These columns don't currently have unique values." and execution stops on line 18 (in the above example). As to why they don't have unique values I believe lies in the SQL that derives the data.

Thanks again for your help!

KK
Mar 19 '08 #5
Well, I finally figured it out! It's a rather clever solution, if I do say so myself. I ended up creating a unique value by concatenating the Style & Color columns in my query, then relating those values to each other in the resulting record sets.

(e.g.: Dim DASizes As New SqlClient.SqlDataAdapter("SELECT DISTINCT Size, Color + Style AS csVal FROM tblName", cnConnection)
and then add a

DSName.Relations.Add("ColorToSize", DSName.Tables("dtColors").Columns("scVal"), DSName.Tables("dtSize").Columns("scVal")))

Then just iterate through the recordset to populate the third branch of the treeview.

It works perfectly!

Again, Thank you for all of your assistance. I look forward to participating in these forums as there is some very useful information here, and I am confident that I will be able to contribute.

Cheers!

KK
Mar 19 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Saso Zagoranski | last post by:
Hi! I would like to know whether there is a component included in .net fw, which resembles Windows explorer table, where you have: filename file size ... Or has anyone else created...
7
by: Sharon | last post by:
I have successfully loaded a DataSet object with a XML schema (XSD). Now I wish to populate the tables that was created in the DataSet. I have an XML file/string that contain all the needed data...
9
by: Marina | last post by:
Hello! I have a database with some customers. Each one of them has 3 other customers under him etc. (something like an MLM system). Now, I want to create a treeview with this. Eg. C1 C11...
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...
6
by: Beginner | last post by:
Hi, I'm trying to populate a TreeView from an existing and filled in ListView (lvwBuild). lvwBuild is a basic two column listview which can have any number of rows. I would like the first...
0
by: Patrick | last post by:
Hi, Im trying to populate my treeview control onmy webpage with the information that is stored in a xml file, but i cant seem to find any good references about it. The xml file looks like this. ...
0
by: AccessDev | last post by:
I have a table called customer and need to populate a treeview. I have looked pretty much everywhere and I have some examples of how to do this in VB6 but not in VB.NET I would appreciate some...
0
petepell
by: petepell | last post by:
Hello all, I am developing an application in VB 2008 that works with a SQL2005 DB to store and manipulate employee data. In one section of the app I want to be able to show a treeview of the...
0
KodeKrazy
by: KodeKrazy | last post by:
I have a table with the following columns, ID (A unique value) Style#, Color, Size (There a more columns than that, but those are the ones I will need for this exercise.) There is a row for each...
1
nurikoAnna
by: nurikoAnna | last post by:
Goodday... Please help me to populate data in a list view...call data from the database.. Currenty i am using MySQL as a database.... Ex. I have a database name dbSection..Then I have a...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
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,...

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.