I'm looking at this control, and it seems to be lacking the standard .net "datasource" and "datamember" properties for databinding. Is this control not bindable? I can write some custom function that populates the treeview from a given data source, I suppose, and embed data objects as necessary, but is that the 'best practice'? Or does everyone simply use a 3rd party treeview control?
Add nodes. We add a TreeView control to the Windows Forms Application project. To do this, open the Toolbox panel by clicking on the View and then Toolbox menu item in Visual Studio. Double-click on the TreeView item. Now Double-click on the Form1 window in the designer so you can create the Form1_Load event.
Step 1: Adding TreeView to the ApplicationCreate a new Windows Forms Application. In the Design view, drag and drop the C1TreeView control to the form from the Toolbox. The TreeView control appears with Column1 added by default. Click the Smart Tag, and select Dock in Parent Container.
You are correct in that there is no data binding. The reason being is that TreeViews are hierarchical data structures. That is, not a straight list. As a result the databind option is invalid to say a List structure.
Sadly it's creating your own populate methods or buying 3rd party controls (which in the end will have their own populate methods.)
Here's a decent MSDN article on Binding Hierarchical Data.
I use the tree control from Developer's Express. It will take a table of data and display/edit it in a hierarchical fashion.
All it needs is a primary key field and a parent id field in the table and it can figure out what goes where.
You can do the same thing if you roll your own code and use your own class.
class Node
{
System.Collections.Generic.List<Node> _Children;
String Description;
void Node()
{
_Children = new System.Collections.Generic.List<Node>();
}
public System.Collections.Generic.List<Node> Children()
{
return (_Children);
}
}
class Program
{
static void Main(string[] args)
{
System.Collections.Generic.List<Node> myTree = new System.Collections.Generic.List<Node>();
Node firstNode = new Node();
Node childNode = new Node();
firstNode.Children().Add(childNode);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With