TreeView Databind - from c# object?
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!
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
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.
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
精彩评论