Welcome! Log In Create A New Profile

Advanced

How to make a treeview with parents

Posted by Carlo Hermus 
Carlo Hermus
How to make a treeview with parents
January 06, 2010 11:55PM
Hi all,

I thought I share this knowledge I gained about tree and parents.

I have 1 file named contacts. With a ID int and IDParent int field.

The IDParent contains the ID of the Parent in the same file.

To let an entry be a parent of a parent of a parent etc.. I found it was very easy after reading the help instead of coding with trial and error. This way also runs the query ones so, the filling of the treeview is very fast. (I only tested it with a few hundred entries, so if anyone wants to try with more entries and let me (us) know how fast/slow this becomes it would be much appreciated. I hope it is very helpful and if it can be fine-tuned I love to hear it.

So let us start.

I created a query of the datafile called QRY_TreeContacts.

SELECT 
	Contacts.ID AS ID,	
	Contacts.IDParent AS IDParent,	
	Contacts.Name AS Name,	
	Contacts.IsDeleted AS IsDeleted,	
	Contacts.IDIcon AS IDIcon
FROM 
	Contacts
ORDER BY 
	IDParent ASC


My Query 
sRoot is string
sTrash is string
sInbox is string

sParentNode is string

sRoot 	= "Categories"
sTrash 	= "Deleted Items"

sParentNode = ""

TreeDeleteAll(TREE_CONTACTS)
HReadSeek(Icons,ID,1,hIdentical)
TreeAdd(TREE_CONTACTS,sRoot,Icons.Icon,Icons.Icon,0)
HReadSeek(Icons,ID,-11,hIdentical)
TreeAdd(TREE_CONTACTS,sTrash,Icons.Icon,Icons.Icon,-11)

HExecuteQuery(QRY_TreeContacts)
HReadFirst(QRY_TreeContacts)

WHILE NOT HOut()
	
	HReadSeek(Icons,ID,QRY_TreeContacts.IDIcon,hIdentical)
	sParentNode = QRY_TreeContacts.Name
	
	IF NOT QRY_TreeContacts.IDParent = 0 THEN
		sParentNode = TreeFind(TREE_CONTACTS, QRY_TreeContacts.IDParent)
		TreeAdd(TREE_CONTACTS,sParentNode+TAB+QRY_TreeContacts.Name,Icons.Icon,Icons.Icon,QRY_TreeContacts.ID,tvAlphaSort+tvAcceptDuplicate)
	ELSE
		IF NOT QRY_TreeContacts.IsDeleted THEN
			TreeAdd(TREE_CONTACTS,sRoot+TAB+QRY_TreeContacts.Name,Icons.Icon,Icons.Icon,QRY_TreeContacts.ID,tvAlphaSort+tvAcceptDuplicate)
		ELSE IF QRY_TreeContacts.IsDeleted THEN
			TreeAdd(TREE_CONTACTS,sTrash+TAB+QRY_TreeContacts.Name,Icons.Icon,Icons.Icon,QRY_TreeContacts.ID,tvAlphaSort+tvAcceptDuplicate)	
		END
	END
	HReadNext(QRY_TreeContacts)
END

TreeSort(TREE_CONTACTS,sRoot,tvUp)
Alexander S.
Re: How to make a treeview with parents
January 27, 2013 08:00PM
thank you for your post very much!!! I spent a lot of time to search a solution.
Carlo Hermus
Re: How to make a treeview with parents
January 31, 2013 03:28PM
Alexander,

You are welcome. I am happy I can help as I am helped by others.
Author:

Your Email:


Subject:


Spam prevention:
Please, enter the code that you see below in the input field. This is for blocking bots that try to post this form automatically. If the code is hard to read, then just try to guess it right. If you enter the wrong code, a new image is created and you get another chance to enter it right.
Message: