Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you data bind a TreeView control?

Typically, when I use the standard TreeView control that comes with C#/VB I write my own methods to transfer data in and out of the Tree's internal hierarchy store.

There might be ways to "bind" the GUI to a data store that I can point to (such as XML files), and when the user edits the tree items, it should save it back into the store. Is there any way to do this?

like image 772
Robin Rodricks Avatar asked Jun 13 '09 06:06

Robin Rodricks


People also ask

How do I bind data to TreeView?

To bind local data to the TreeView, you can assign a JavaScript object array to the dataSource property. The TreeView component requires three fields (ID, text, and parentID) to render local data source.

How do I get data from TreeView database?

Populating TreeView from database Inside the PopulateTreeView method, a loop is executed over the DataTable and if the ParentId is 0 i.e. the node is a parent node, a query is executed over the VehicleSubTypes table to populate the corresponding child nodes and again the PopulateTreeView method is called.

How many root nodes can a TreeView control have?

A typical tree structure has only one root node; however, you can add multiple root nodes to the TreeView control. The Nodes property can also be used to manage the root nodes in the tree programmatically. You can add, insert, remove, and retrieve TreeNode objects from the collection.


1 Answers

I got around it by creating a class that Inherits TreeNode and contains an object. you can then bind a record to the node and recall it during the Click or DoubleClick event. Eg.

class TreeViewRecord:TreeNode
    {
        private object DataBoundObject { get; set; }

        public TreeViewRecord(string value,object dataBoundObject)
        {
            if (dataBoundObject != null) DataBoundObject = dataBoundObject;
            Text = value;
            Name = value;
            DataBoundObject = dataBoundObject;
        }

        public TreeViewRecord()
        {
        }

        public object GetDataboundObject()
        {
            return DataBoundObject;
        }
    }

then you can bind to each node as you build your TreeView eg.

TreeView.Nodes.Add(new TreeViewRecord("Node Text", BoundObject));
//or for subNode
TreeView.Nodes[x].Nodes.Add(new TreeViewRecord("Node Text", BoundObject));

Then you can bind the DoubleClick event to something like this

private void TreeViewDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
        {
             object exp = ((TreeViewRecord) e.Node).GetDataboundObject();
             //Do work
        }
like image 110
Kezzla Avatar answered Oct 03 '22 06:10

Kezzla