Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

TreeView

Status
Not open for further replies.

Westond

Programmer
Jan 19, 2005
43
0
0
US
I have a page that needs to have a treeview that is populated from a database. Below is a little bit of code that i have been playing with. I would like the data in the hashtables to look like this if it were to work correctly.

Parent1
- Child 1
- Lvl 1
Parent2
- Child 2
- Lvl 2

and so on....

All the data I have is coming from different datatables for each lvl Parent child ect. Not like it s displayed below with hashtables.

Hashtable x = new Hashtable();
Hashtable y = new Hashtable();
Hashtable z = new Hashtable();

TreeNode Parent = new TreeNode();
TreeNode Child = new TreeNode();

x.Add(0, "Parent 1");
x.Add(1, "Parent 2");
x.Add(2, "Parent 3");
x.Add(3, "Parent 4");

y.Add(0, "Child 1");
y.Add(1, "Child 2");
y.Add(2, "Child 3");
y.Add(3, "Child 4");

z.Add(0, "Lvl 1");
z.Add(1, "Lvl 2");
z.Add(2, "Lvl 3");
z.Add(3, "Lvl 4");


for (int i = 0; i <= x.Count - 1; i++)
{
Parent = new TreeNode(x.ToString(), "Value");

for (int j = 0; j <= y.Count - 1; j++)
{
Child.ChildNodes.Add(new TreeNode(z[j].ToString(), "Val"));
Parent.ChildNodes.Add(new TreeNode(y[j].ToString(), "ChildValue"));
}
TreeView1.Nodes.Add(Parent);
}

Thanks for any help or advise!
 
i would suggest having multi dimensional hashtables.

e.g:
Code:
    Function GenerateHash(ByVal strParentName As String, ByVal strChildString As String, ByVal strLevelString As String) As Hashtable
        Dim x As New Hashtable
        Dim y As New Hashtable
        Dim z As New Hashtable

        z.Add(0, strLevelString)
        y.Add(0, strChildString)
        y.Add(1, z)
        x.Add(0, strParentName)
        x.Add(1, y)

        Return x

    End Function


    'Implementing the function
        Dim MasterHash As New Hashtable

        MasterHash.Add(0, GenerateHash("Parent1", "Child1", "Level1"))
        MasterHash.Add(1, GenerateHash("Parent2", "Child2", "Level2"))

        'Take back data
        Dim x As New Hashtable
        Dim y As New Hashtable
        Dim z As New Hashtable
        For intParent As Integer = 0 To MasterHash.Count - 1
            x = CType(MasterHash(intParent), Hashtable)
            Response.Write(x(0) & "->")
            y = CType(x(1), Hashtable)
            Response.Write(y(0) & "->")
            z = CType(y(1), Hashtable)
            Response.Write(z(0) & "<br>")
        Next

PS: the code is in VB.NET ;), the language that i can give you an immediate soultion in...

Known is handfull, Unknown is worldfull
 
I am tring to get all the information into a treeview so it is not actually displayed as I put it above with the "-".

 
the display was just an example. you have to create the nodes in the for loop.

e.g.:

For intParent As Integer = 0 To MasterHash.Count - 1
dim ParentNode as new Node
dim Child as new Node
dim Level as new Node

x = CType(MasterHash(intParent), Hashtable)
ParentNode.text=x(0)
y = CType(x(1), Hashtable)
ChildNode.text=y(0)
z = CType(y(1), Hashtable)
LevelNode.text=z(0)

ChildNode.Add(LevelNode)
ParentNode.Add(ChildNode)
Tree.Add(ParentNode)
Next

Note:
the node object maye actually have different methods than the one that i have used above. the code above is a sample...

Known is handfull, Unknown is worldfull
 
thanks for your help vbkris. I have 1 last problem, I see how it works in the code you displayed. My problem is I have an unknown lvl in the tree. The user is able to add more lvls so I wouldn't be able to dim parent, child, lvl because it could be parent, child, lvl, lvl2, lvl3.... There shouldn't be more the 4 lvls to the tree but you see where I am coming from. Is treeview the best way to go about this?

THANKS
 
yes it is. give me sometime so that i can cook up a better method to create the hashtables...

Known is handfull, Unknown is worldfull
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top