Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

treeView from dataSet

Posted on 2004-09-30
5
662 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
ID: 12189320
Google TVirtualTreeview...
0
 
LVL 6

Accepted Solution

by:
david_barker earned 150 total points
ID: 12189876
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
ID: 12190011
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
ID: 12190216
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
ID: 12190360
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Using idhttp to login to instagram 2 106
Working with hours 3 65
Tvertscrollbox like a whatsapp layout 5 34
scroll down TListBox component in Delphi 1 19
A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

861 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