Hi,
Isn't there a problem here with child 15?
It has two entries in the data and is self referential.
I assume it is a typo although the heirarchy makes it look
intentional. A way of declaring a leaf node?
Anyway, stepping through Nicholas's logic.
He is suggesting a dictionary of nodes.
Each node has a child node collection.
i.e. each node is a tree in itself.
So
Step 1
Create a node. Attach your first row of data to it as a payload.
You can use the tag property for this.
Get the child value of this row of data from the data and use it as
the key for your dictionary entry.
Your dictionary will be keyed on an integer and its value will be a
treenode (System.Collections.Generics.Dictionary)
So...
treenode t = new treenode();
dictionary(<int>,<treenode>) d = new dictionary(<int>,<treenode>) ();
t.tag = myfirstdataobject //be it a string made from the data or a
class with the data in it. Up to you what you want to hang on the
tree.
d.add(1,t); in goes the first node
Now the next step is
make a new treenode say t1 and attach the second row of data to it.
Get its parent id from the data.
Ask the dictionary if it has the parent.
Seeing the parent id of the second data row is 1 then yes the
dictionary has it.
Add t1 to the node collection of t. other wise just add t1 to the
dictionary
if (d.containskey(1))
d[1].nodes.add(t1); //We are adding t1 into the nodes
collection of its parent
//now we stick t1 into the dictionary.
d.add(2,t1); //The key is two because that is the value retrieved from
your data, not because this is the second entry.
This process will work until you get to the second data row with 15 as
a child id. This will break the dictionary when you try to add it.
Don't know what you do here. Depends on the meaning of the data as to
what your options are.
you will only need a single pass with your perfectly sorted data.
but with unsorted data a number of passes would be necessary.
Once you have all of your nodes processed in the dictionary hang them
on your treeview.
To get your nodes from the dictionary to the tree you need to clone
the root nodes and add them to the treeview nodes collection.
as per this snippet where I have represented your data with class
myDummyload
private void BuildTree()
{
myDummyload dl1 = new myDummyload(1, 0);//childid,parentid
myDummyload dl2 = new myDummyload(2, 1);
List<myDummyload> myDataCollection = new
List<myDummyload>();
myDataCollection.Add(dl1);
myDataCollection.Add(dl2);
Dictionary<Int32,TreeNode> d = new
Dictionary<Int32,TreeNode>();
IEnumerator<myDummyload> en =
myDataCollection.GetEnumerator();
while (en.MoveNext())
{
TreeNode t = new TreeNode("I am node " +
en.Current.ChildId.ToString());
t.Tag = en.Current;
if (d.ContainsKey(((myDummyload)t.Tag).ParentId))
d[((myDummyload)t.Tag).ParentId].Nodes.Add(t);
d.Add(((myDummyload)t.Tag).ChildId, t);
}
IEnumerator<TreeNode> en1 = d.Values.GetEnumerator();
while (en1.MoveNext())
if (en1.Current.Parent == null)
this.treeView1.Nodes.Add((TreeNode)en1.Current.Clone());
}
Once you exit this code your treeview should be displaying your nodes
hth
bob
I'm really new at this C#. I'm not sure I'm understanding your
description. It sounds like it will populate a table in a Hierarchy
order. I am not understanding how this populates a treeview? I tried
writing some pseudocode but was completely lost. Could you provide a
little more detail for this novice.
Thanks
On Tue, 16 Oct 2007 22:08:52 -0400, "Nicholas Paldino [.NET/C# MVP]"
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]
- (e-mail address removed)
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]"
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]
- (e-mail address removed)
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 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