Solved

treeView from dataSet

Posted on 2004-09-30
5
667 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
[X]
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
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

691 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