views:

169

answers:

2

I'm trying to build the server/network settings system for my IRC client. Because of the relational nature of the networks and servers I opted to use SQLite to store everything.

My first step is making the settings dialog, which is like you might expect; a TreeView that holds the networks and server, and a space on the side to edit their settings.

I have a (tiny) little experience with ADO.NET, but just basic selecting and updating and nothing relational. So my problem is figuring out how to associate a row to a TreeNode. I already have custom TreeNode objects for servers and networks and it all works well with some manually-entered data.

So I guess I don't need to "bind" per-se, I just need load the data into the TreeView and be able to update the settings easily.

+1  A: 

In our projects , We usually use the Tag property of TreeNode to associate a row with a TreeNode and we load data manually into TreeView without binding.

Beatles1692
+1: Using the Tag attribute works well for me too.
Walt W
+2  A: 

The main issue when populating a Treeview is whether you do a complete load of all nodes or implement some sort of lazy load approach as each node is expanded.

Populating the complete tree is only going to perform acceptably if you have less then about 500 nodes, and the number of nodes will not grow. Otherwise the lazy loading approach is best.

The simplest way to do this is to populate the children of a node in the BeforeExpand event. However the TreeView will not display a +/- indicating a node has children, until you actually add children to it. Therefore as you populate a parent node, you should check if the logical item (data row) has children and if it does, add a dummy node.

I've found inheriting a custom class from TreeNode is one of the easiest approaches to defining a dummy node. Then you can check for it in BeforeExpand() using the Is operator.

Then, when the user expands the parent node, in the BeforeExpand event, check if there is a dummy node, if there is delete it and then populate the actual nodes. For each of these nodes check for children and populate the tag property and a dummy node if required.

Set the Tag property for each node to some sort of meaningful object/data. This will allow you to then easily populate child nodes when needed. Just be aware that it is of type object, you will need to correctly cast it to the actual data type when needed.

Ash
I hadn't considered the "lazy loading", as you put it, thanks.About the Tag property, I already have custom TreeNode objects so it's not really necessary. That way I can easily tell what was clicked and which textboxes need to be displayed on the other side of the form.On the db side, I'm still not sure what I would set the Tag property to as you suggest.
Brian Ortiz