[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 685
  • Last Modified:

treeView from dataSet

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
krypto2000
Asked:
krypto2000
  • 2
  • 2
1 Solution
 
Wim ten BrinkCommented:
Google TVirtualTreeview...
0
 
david_barkerCommented:
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
 
krypto2000Author Commented:
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
 
david_barkerCommented:
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
 
krypto2000Author Commented:
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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now