#1
  1. Huh?
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Location
    Scotland, UK
    Posts
    223
    Rep Power
    11

    tree views in BCB


    Hey Folks!

    I am using tree views in BCB V6 for the first time.

    They seem easy enough to use normally..ie add items and subitems.

    What i want to know is whether it is possible to populate it with items taken dynamically from a database.

    I have no code for trying this as i have not yet attempted it. I just need to know if it is possible and a starting point for how to do it if it is possible.

    For example, if my database had:

    item1
    subitem 1.1
    subitem 1.2
    subitem 1.3

    item 2
    subitem 2.1
    subitem 2.2

    This would automatically be displayed i the tree view. However, if i then added item 3 to my database it would have to automatically come up on screen without me manually adding it to the treeview.

    i hope this makes sense, if not just ask me to clear up any points and i will try!

    Thanks!

    Thought for the day:
    A winner never quits, a quitter never wins.
    -----------------------------------------------------------
    4dr14n
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,594
    Rep Power
    4207
    You could try using a timer to periodically poll your database, to see if any new items have been added/deleted.
  4. #3
  5. Huh?
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Location
    Scotland, UK
    Posts
    223
    Rep Power
    11
    Thanks for the reply.

    However, i dont think i made myself clear...

    i need to know how to get the data from the database to appear in the tree view. At this point it does not matter if the database has been changed while it is being viewed.

    I do not want the tree view items to be hardcoded, they must reflect items that are in a table in my database, and as these items can change i need code that will take whatever is in the database and put it in the treeview.

    I hope this clears it up slightly but if not just ask!
    -----------------------------------------------------------
    4dr14n
  6. #4
  7. No Profile Picture
    Offensive Member
    Devshed Novice (500 - 999 posts)

    Join Date
    Oct 2002
    Location
    in the perfect world
    Posts
    622
    Rep Power
    27
    Items can be inserted into a treeview with a SendMessage() and TVM_INSERTITEM msg. Fill in a TV_INSERTSTRUCT struct first.

    An item is usually inserted after the last item ie

    TV_INSERTSTRUCT TreeData;
    TreeData.hInsertAfter=TVI_LAST;

    To set the item as having root level (the first level, no a child of another item) use

    TreeData.hParent=TVI_ROOT;

    insert the item and catch the return as it is the HANDLE (HTREEITEM) of the new item. Check that it is a valid handle or call GetLastError()

    hPrev=(HTREEITEM)SendMessage(hTreeView, TVM_INSERTITEM,0 ,(LPARAM)(LPTV_INSERTSTRUCT) &TreeData);
    The essence of Christianity is told us in the Garden of Eden history. The fruit that was forbidden was on the Tree of Knowledge. The subtext is, All the suffering you have is because you wanted to find out what was going on. You could be in the Garden of Eden if you had just kept your f***ing mouth shut and hadn't asked any questions.

    Frank Zappa
  8. #5
  9. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,594
    Rep Power
    4207
    You could use TechNoFear's methods if you want, but BCB actually has wrapped most of those methods in the TTreeView object. You can call TreeView1->Items->AddChild() method to add children to a node and recursively call the function to build a tree. Here's some code to do so using BCB. Note that I just wrote the code, I haven't actually tested it, since I don't have a BCB compiler installed on this particular computer. However, the logic should be very easy to understand.
    [edit]
    Code:
    void __fastcall TForm1::Button1Click(TObject *Sender)
    {
    	BuildNodes(NULL, 0);
    }
    //---------------------------------------------------------------------------
    void TForm1::BuildNodes(TTreeNode *parent, int parent_id)
    {
    	TQuery *qry;
    	char sql[1024];
    	AnsiString name;
    	int id;
    	TTreeNode *Node;
    
    	qry = new TQuery(this);
    	qry->DatabaseName = "Database1";
    	qry->SQL->Clear();
    	sprintf(sql, "SELECT id, name FROM table WHERE parent_id = %d", parent_id);
    	qry->SQL->Add(sql);
    	qry->Open();
    	// Might need to uncomment the next two lines
    	// for certain DB engines, that lock tables when the result 
    	// set is cached on the server end, until the client 
    	// fetches all the results.
    	// qry->Last();
    	// qry->First();
    	while (!qry->Eof) {
    		id = qry->FieldByName("id")->AsInteger;
    		name = qry->FieldByName("name")->AsString;
    		Node = TreeView1->Items->AddChild(parent, name);
    		// Now recurse into function to build children
    		BuildNodes(Node, id);
                                    
    		// Get the next sibling
    		qry->Next();
    	}
    	qry->Close();
    	delete qry;
    }
    [/edit]
    Hope this helps :)
    Last edited by Scorpions4ever; July 1st, 2003 at 12:56 AM.
  10. #6
  11. Huh?
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Location
    Scotland, UK
    Posts
    223
    Rep Power
    11
    Thanks for the info...

    Managed to do it using a similar method to what Scorpions4ever suggested. My database design is now causing me a bit of a headache..but thats a story for another day!

    Thanks for the help.
    -----------------------------------------------------------
    4dr14n

IMN logo majestic logo threadwatch logo seochat tools logo