473,396 Members | 1,997 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,396 software developers and data experts.

How to populate a treeview from a dataset

How to populate a treeview from a dataset

I am very new to C#. I need to create a Windows form that will read a SQL table and populate a treeview. I can connect to the DB, create the dataadapter, populate a data set. The problem is how to use the dataset to populate a treeview.

I have looked at a few examples but none use a dataset, or the data structure was different and I could not modify to work with my data or the examples were more than I needed and too complex for a beginner. Can some one suggest a URL, book or show me an example of code that simply takes the data as shown and populates a treeview.

I would really appreciate the help.

Thank you.


I have a SQL Server 2005 table containing this data shown below.

Child Parent depth Hierachy
1 NULL 0 01
2 1 1 01.02
5 2 2 01.02.05
6 2 2 01.02.06
3 1 1 01.03
7 3 2 01.03.07
11 7 3 01.03.07.11
14 11 4 01.03.07.11.14
12 7 3 01.03.07.12
13 7 3 01.03.07.13
8 3 2 01.03.08
9 3 2 01.03.09
4 1 1 01.04
10 4 2 01.04.10
15 NULL 0 15
15 15 1 15.15
16 15 1 15.16
18 16 2 15.16.18
17 15 1 15.17
Oct 16 '07 #1
3 2246
vanc
211 Expert 100+
How to populate a treeview from a dataset

I am very new to C#. I need to create a Windows form that will read a SQL table and populate a treeview. I can connect to the DB, create the dataadapter, populate a data set. The problem is how to use the dataset to populate a treeview.

I have looked at a few examples but none use a dataset, or the data structure was different and I could not modify to work with my data or the examples were more than I needed and too complex for a beginner. Can some one suggest a URL, book or show me an example of code that simply takes the data as shown and populates a treeview.

I would really appreciate the help.

Thank you.


I have a SQL Server 2005 table containing this data shown below.

Child Parent depth Hierachy
1 NULL 0 01
2 1 1 01.02
5 2 2 01.02.05
6 2 2 01.02.06
3 1 1 01.03
7 3 2 01.03.07
11 7 3 01.03.07.11
14 11 4 01.03.07.11.14
12 7 3 01.03.07.12
13 7 3 01.03.07.13
8 3 2 01.03.08
9 3 2 01.03.09
4 1 1 01.04
10 4 2 01.04.10
15 NULL 0 15
15 15 1 15.15
16 15 1 15.16
18 16 2 15.16.18
17 15 1 15.17
You can read through data to manually add nodes into treeview one by one. You can use datareader to do this.
e.g. if(reader.HasRow)
while(reader.Read())
treeview.nodes.add(reader["field name"].ToString());
cheers.
Oct 16 '07 #2
[quote=vanc]You can read through data to manually add nodes into treeview one by one. You can use datareader to do this.
e.g. if(reader.HasRow)
while(reader.Read())
treeview.nodes.add(reader["field name"].ToString());
cheers.[/QUOTE


If I use that technique if a branch has say 4 levels but the second level node has children how do I get back to that node to populate it’s children? Like in the example:
Child Parent
1..... null
2..... 1
3..... 2
4..... 3
5..... 2
6..... 5

If I simply add nodes one at a time from the data reader, when I get to 5 I am lost. Have I missed something?
Oct 17 '07 #3
vanc
211 Expert 100+
[quote=kirknew2SQL]
You can read through data to manually add nodes into treeview one by one. You can use datareader to do this.
e.g. if(reader.HasRow)
while(reader.Read())
treeview.nodes.add(reader["field name"].ToString());
cheers.[/QUOTE


If I use that technique if a branch has say 4 levels but the second level node has children how do I get back to that node to populate it’s children? Like in the example:
Child Parent
1..... null
2..... 1
3..... 2
4..... 3
5..... 2
6..... 5

If I simply add nodes one at a time from the data reader, when I get to 5 I am lost. Have I missed something?
Since every node has its value, you can check its value from the root node to the end node and stop and the node with the appropriate value to add child node to it.
Oct 26 '07 #4

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

Similar topics

0
by: T.H.M | last post by:
This is the all code. Very simple and short. I need to populate a treeView in aspx page (web form) from XML file. I get en completion error:No overload for method 'TreeNode' takes '1' arguments ....
4
by: Mike | last post by:
how can i populate a treeview from a database? websites with examples would help.
0
by: Björn Bengtsson | last post by:
Hello! I have an urgent problem concerning ASP.NET, ADO.NET, SQL Server, XML and the TreeView control. I have two tables; one describing the products and one describing their relationships. A...
3
by: Yul | last post by:
Hi, We are in the process of designing an ASP.NET app, where a user will enter some 'Customer ID' to be queried in the database. If the ID is valid, several stored procedures will be called to...
1
by: john wright | last post by:
I have my code done to populate a treeview, but when I move it to the backgroundworker I get an error "Action being performed on this control is being called from the wrong thread. Marshal to the...
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. ...
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...
0
by: xmail123 | last post by:
How to populate a treeview from a dataset I am very new to C#. I need to create a Windows form that will read a SQL table and populate a treeview. I can connect to the DB, create the...
0
by: Dennis Francek | last post by:
Hello I have populated a treeview from my dataset and by clicking in the treeview i want to get the corresponding table shown in a datagridview. Ive managed this by looking at each DataRow and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
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
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
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...
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
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...

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.