Adding 'object' to Treeview?

E

Ed Minrights

I have a bunch of related classes that I want to add into a Treeview
with a hierarchy. However, Treeview doesn't support the insertion of
the root 'object' type into the nodes, which surprises me. I just
wanted to add them in and have the mouse events fire back with the
reference to the object clicked on. I figured I'd have to override
ToString in my classes to have them show up in the treeview in the
right way and that would be that.

But to my surprise it doesn't seem to work like that and it seems like
I have to start messing with data binding... which just seems so
overkill for the task that it must be the wrong thing to do.

Can't I just add 'object' types like I can to the collection classes?
What should I be doing instead?

Thanks for any help!
 
N

Nicholas Paldino [.NET/C# MVP]

Ed,

Collections and the visual tree control are massively different
constructs.

The easiest way to do this would be to set the Tag property of the node
that refers to your object with a reference to your object.

Then, if you want to use the properties of the object in the tree, then
you can override the ToString method on your objects and display that, or
have some other common interface that they share which will give you the
description details.

Hope this helps.
 
E

Ed Minrights

Ed,

Collections and the visual tree control are massively different
constructs.

The easiest way to do this would be to set the Tag property of the node
that refers to your object with a reference to your object.

Then, if you want to use the properties of the object in the tree, then
you can override the ToString method on your objects and display that, or
have some other common interface that they share which will give you the
description details.

Cheers, I'll give that a shot. At the moment I've got the luxury of
being able to derive my classes from TreeNode and pop them directly
in. The constructor overwrites TreeNode.Text with the relevant string.

It's not ideal and it isn't going to work when I need to derive from
something else.
 
D

David

Cheers, I'll give that a shot. At the moment I've got the luxury of
being able to derive my classes from TreeNode and pop them directly
in. The constructor overwrites TreeNode.Text with the relevant string.
This sounds like what I am having trouble with. I have a TreeView that
works fine when I do:

Nodes.Add(new TreeNode);

but gives me an error from within TreeNode.LoadViewState when I use

Nodes.Add(new DerivedTreeNode());

defined

public class DerivedTreeNode : TreeNode
{
public DerivedTreeNode() : base()
{
Text = "Some Text";
}
}

Any clue as to what is happening?

What I really want to do is put a WebControl in the place of the TreeNode
text. I'm surprised that there isn't a simple way to do so. Or is there?
 
E

Ed Minrights

Ed,

Collections and the visual tree control are massively different
constructs.

The easiest way to do this would be to set the Tag property of the node
that refers to your object with a reference to your object.

Then, if you want to use the properties of the object in the tree, then
you can override the ToString method on your objects and display that, or
have some other common interface that they share which will give you the
description details.

That worked perfectly as was exactly what I was after in the first
place. I was getting a little misled by the Add method of the
TreeNodeCollection since it talks about attaching a 'key' to an entry
as well as the text.. albeit only as a String again.

Thanks very much!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top