Solved

treeView from dataSet

Posted on 2004-09-30
5
658 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
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.
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 …

929 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

15 Experts available now in Live!

Get 1:1 Help Now