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.)
5 3762
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
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: -
-
Dim DSProducts As DataSet
-
Dim CNproducts As New SqlClient.SqlConnection("DATA SOURCE=.\SQLDBNAME; INITIAL CATALOG=dbTableName; Integrated Security = True")
-
Dim DAItems As New SqlClient.SqlDataAdapter("SELECT DISTINCT Style, Description FROM Items WHERE Catalog = 4", CNproducts)
-
Dim DAColors As New SqlClient.SqlDataAdapter("SELECT DISTINCT Color, Style FROM Items WHERE Catalog = 4", CNproducts)
-
Dim DASizes As New SqlClient.SqlDataAdapter("SELECT DISTINCT Size, SizeOrder, Style, Color from Items WHERE Catalog = 4", CNproducts)
-
-
DSProducts = New DataSet()
-
CNproducts.Open()
-
DAItems.Fill(DSProducts, "dtItems")
-
DAColors.Fill(DSProducts, "dtColors")
-
DASizes.Fill(DSProducts, "dtSize")
-
-
'Create a data relation object to facilitate the relationship between the Colors and Sizes data.
-
DSProducts.Relations.Add("ItemToColor",DSProducts.Tables("dtItems").Columns("Style"),DSProducts.Tables("dtColors").Columns("Style"))
-
-
' Tried both of these.Neither relation returns any records.
-
DSProducts.Relations.Add("ItemToSize",DSProducts.Tables("dtItems").Columns("Style"),DSProducts.Tables("dtSize").Columns("Style"))
-
'DSProducts.Relations.Add("ColorToSize",DSProducts.Tables("dtColors").Columns("Color"),DSProducts.Tables("dtSize").Columns("Color"))
-
-
TreeView1.Nodes.Clear()
-
Dim parentrow As DataRow
-
Dim ParentTable As DataTable
-
ParentTable = DSProducts.Tables("dtItems")
-
-
For Each parentrow In ParentTable.Rows
-
-
Dim parentnode As TreeNode
-
parentnode = New TreeNode(parentrow.Item(0) & " " & parentrow.Item(1))
-
TreeView1.Nodes.Add(parentnode)
-
' populate child
-
-
Dim childrow As DataRow
-
Dim childnode As TreeNode
-
childnode = New TreeNode()
-
For Each childrow In parentrow.GetChildRows("ItemToColor")
-
childnode = parentnode.Nodes.Add(childrow(0))
-
childnode.Tag = childrow("Color")
-
-
' populate child2
-
-
Dim childrow2 As DataRow
-
-
Dim childnode2 As TreeNode
-
childnode2 = New TreeNode()
-
For Each childrow2 In childrow.GetChildRows("ItemToSize")
-
childnode2 = childnode.Nodes.Add(childrow2(0))
-
childnode2.Tag = childrow("Size")
-
-
Next childrow2
-
-
Next childrow
-
-
Next parentrow
-
-
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
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
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
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
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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.
...
|
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...
|
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...
|
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...
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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,...
| |