Solved

treeView from dataSet

Posted on 2004-09-30
5
654 Views
Last Modified: 2010-04-05
Hello,

I want to make a treeView from a dataSet

this is my values :
 ----------------------------------------
| wks_desc | sct_desc | type_desc |
 ----------------------------------------
|Emboitage | stk         | montres   |
|Emboitage | ipg         | montres   |
|Certissage | stk         | montres   |
|Certissage | stk         | plaques    |
|Certissage | ipg         | montres   |
|Certissage | ipg         | plaques    |  
 ----------------------------------------  okay....

with these data, i want to have a treevView like this

 + Workshop
    + ---Emboitage
       + ---stk
           |---montres
       + ---ipg
           |---montres
    + ---Certissage
        + ---stk
            |---montres
            |---plaques
        + --- ipg
            |---montres
            |---plaques

that's all !! please help !!! :-O

0
Comment
Question by:krypto2000
  • 2
  • 2
5 Comments
 
LVL 17

Expert Comment

by:Wim ten Brink
Comment Utility
Google TVirtualTreeview...
0
 
LVL 6

Accepted Solution

by:
david_barker earned 150 total points
Comment Utility
Or without googling :

Add to the form a TreeView named TreeView1, a DataSet (I chose an ADO one) named ADODataSet1 and something to call FillTreeView.

procedure TfrmMain.FillTreeView;
var
  nodNext    : TTreeNode;
  iLoop      : integer;

  function AddNode(nodParent : TTreeNode; sValue : string) : TTreeNode;
  var
    iLoop : integer;
  begin
    Result:=nil;
    for iLoop := 0 to TreeView1.Items.Count-1 do
      if  (TreeView1.Items[iLoop].Text=sValue)
        and (TreeView1.Items[iLoop].Parent=nodParent)
          then Result:=TreeView1.Items[iLoop]; // the node already exists, just return it
    // the node doesn't exist, create it
    if Result=nil then Result:=TreeView1.Items.AddChild(nodParent,sValue);
  end;

begin
  TreeView1.Items.Clear;
  while not ADODataSet1.Eof do
  begin
    nodNext:=AddNode(nil    ,ADODataSet1['wks_desc']);
    nodNext:=AddNode(nodNext,ADODataSet1['sct_desc']);
                  AddNode(nodNext,ADODataSet1['type_desc']);
    ADODataSet1.Next;
  end;
end;
0
 

Author Comment

by:krypto2000
Comment Utility
okay that's grate it work !!

i don't understant your logic so if you wan't you can explain me it would be good but i you don't wan't no problem...

thanks !
0
 
LVL 6

Expert Comment

by:david_barker
Comment Utility
This was fairly easy for one main reason : the level of indentation of the items in the TreeView is known in advance ('wks_desc' is always at level 0, 'sct_desc' at level 1, ...)

Here we go for the details :
  - when I call AddNode for the level 0, it doesn't have a parent (top level nodes don't), so I set nodParent to nil.
  - I search through the TreeView to see if a node with same parent already exists or not. If it doesn't, I create it.
  - in any case I return this node (existing or just created) so that the next level item knows who is it's parent.
  - and for the last level, I ignore the return result because there are no more levels.

Writing that gave me the idea of better version of the same code, which will work with any number of fields :

 while not ADODataSet1.Eof do
  begin
    nodNext:=nil;
    for iChamps := 0 to ADODataSet1.FieldCount-1
      do nodNext:=AddNode(nodNext,ADODataSet1.Fields[iChamps].Text);
    ADODataSet1.Next;
  end;

Just make sure that in the query the fields are in the right order !

Cheers,

David
0
 

Author Comment

by:krypto2000
Comment Utility
Okay thank you that's very good !!!

I had a completely different approach, I had almost arrived there but it was surely not the maid ;-)

regards,

Matthias
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now