views:

452

answers:

2

I would like to databind my TreeView from a data structure. After googling some, I came to a conclusion that writing my own simple Tree structure using List of Nodes would be the thing to do. However, I would like to know whether I am on the right path and not overdoing it - from what I know there is no Tree structure in c#.

What are the preferable methods to bind a TreeView to the data? (dynamic of course). Can I do something like:

List<node> nodes = new List<Node>();
//populate the nodes object
myTree.DataSource = nodes;

that would be ideal....Has anyone had any luck binding the tree to an actual object? From what i know so far, i would have to manually add TreeNode nodes to the tree and populate them from my nodes list, which is fine, but if there is an easier way, that would be better.

Thanks!

+1  A: 

MSDN has a good amount of information on binding to the TreeView.

I prefer using the SiteMap to bind data.

Also, you can create nodes through LINQ to XML

Jim Schubert
thanks, SiteMap looks like it might fit here.
gnomixa
+1  A: 

If you're using ASP.NET, you can read up on databinding for TreeViews here.

In winforms, however, TreeView doesn't support databinding. You have to do your own, so your understanding that you have to manually add nodes is correct.

You can implement INotifyPropertyChanged on your underlying data structure and then listen for that event and redraw the TreeView as needed if/when your nodes change.

Anna Lear
I am using web forms. I think INotifyPropertyChanged will be useful for me.As far as Populating Nodes on Demand goes, I do realize I can use this BUT my question referred more to whether it's done automatically or I have to populate the nodes myself. Either I just point the TreeView.DataSource = somesource object OR I populate and add TreeNode objects MYSELF inside the populate node function.
gnomixa