Re: How to populate a treeview from a dataset
- From: "Nicholas Paldino [.NET/C# MVP]" <mvp@xxxxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Tue, 16 Oct 2007 22:08:52 -0400
Well, the one I originally stated would work fine. If you select the data in the order you specified, then you can do the following:
- Create a dictionary, it is keyed on the Child value, and contains the tree node.
- Cycle through the data
- Create a new tree node with the information
- Place it in the dictionary, using the Child value as the key
- Check to see if the Parent id is contained in the dictionary
- If it is then get the node from the dictionary and set the parent of the current node to the parent node.
That's about it.
--
- Nicholas Paldino [.NET/C# MVP]
- mvp@xxxxxxxxxxxxxxxxxxxxxxxxxxx
<xmail123@xxxxxxxxx> wrote in message news:471521f2.13968406@xxxxxxxxxxxx
Sounds simple to an MVP. LOL
THere is what I am guessing I will need to do let me know if this
logic sounds right.
1. From my ds load all the root tree nodes (Parent = Null) into a temp
table.
2. Step through this temp table. Create a node for the first row.
3. Find all the children of the node just created and place them into
a temp table. If no children are found continue with step number 5.
4. Continue with step number 2.
5. Since no children were found move to the next row in the current
table. Continue with step number 2.
6. This process continues until the last root tree node is completely
processed.
I am certainly open to a better approach.
On Tue, 16 Oct 2007 16:05:59 -0400, "Nicholas Paldino [.NET/C# MVP]"
<mvp@xxxxxxxxxxxxxxxxxxxxxxxxxxx> wrote:
You can't directly bind a data source to a tree view. The reason for
this is that most data sources deal with vectors, not heiarchical data sets.
It looks like you are getting the result set ordered by Parent, then
Child, which is good.
What I would do is cycle through this set, and create a new tree view
node with the child id. As you created nodes, place them in a dictionary
with their ids as a key. Then, when you create a child node, get the parent
node (which you have the key for) from the dictionary, and add the child to
it.
--
- Nicholas Paldino [.NET/C# MVP]
- mvp@xxxxxxxxxxxxxxxxxxxxxxxxxxx
<xmail123@xxxxxxxxx> wrote in message news:471513c1.10335203@xxxxxxxxxxxxHow 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 here 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
.
- Follow-Ups:
- Re: How to populate a treeview from a dataset
- From: xmail123
- Re: How to populate a treeview from a dataset
- References:
- How to populate a treeview from a dataset
- From: xmail123
- Re: How to populate a treeview from a dataset
- From: Nicholas Paldino [.NET/C# MVP]
- Re: How to populate a treeview from a dataset
- From: xmail123
- How to populate a treeview from a dataset
- Prev by Date: Re: Collection property and control owner
- Next by Date: Re: ASP, ASP.net, and C#
- Previous by thread: Re: How to populate a treeview from a dataset
- Next by thread: Re: How to populate a treeview from a dataset
- Index(es):
Relevant Pages
|
Loading