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!

0

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

Comments

1

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.

1 Comment

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.
0

Here is a good link that explain how to databind a treeview with the following functionality:

  • Load the data in the tree structure.
  • Keep track of changes in the data, showing them in the control
  • Expose a get/set property like SelectedValue besides SelectedNode, generally you have the value of the node you want to select (but not the node itself!!!)

DataBound TreeView

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.