How do I convert the data in a TStringGrid to a TTreeView

I have a StringGrid containing columns, Building, Floor, Room, Equipment. Thus each of several buildings listed could have several floors and each floor could contain a number of rooms and in each room there could be different pieces of equipment.  The grid could have up to 2000 rows.  I need to convert these tables to a TreeView as per the example below:  

In StringGrid form the data looks like this
Building     Floor     Room     Equipment
Building1   Floor1   Room1   Equipment1
Building1   Floor1   Room1   Equipment2
Building1   Floor1   Room1   EquipmentX
Building1   Floor1   Room2   Equipment1
Building1   Floor1   Room2   Equipment1
etc.

In tree from the data will look as below:
Building1
    Floor1
        Room1
             Equipment1
             Equipment2
             EquipmentX
        Room2
             Equipment1
    Floor2
Building2
Building3

Can any one help with a piece of code or an idea that can solve this.  I tried several methods, one being running down the columns in the grid, one of the other and creating a node in the tree when there is a change, e.g. floor1 changes to floor2, but this falls flat when there is not always a change, e.g when floor 1 has only a room1 and going onto floor2 with also only a room 1.
HenryM2Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Geert GOracle dbaCommented:
you need to check for every column change
and save the previous item for each level for each column

i just typed this in this editor, don't have Delphi here,
i'll test it tomorrow if any problems :)
procedure TForm1.ConvertGridToTreeView(aGrid: TStringGrid; aTreeview: TTreeview);
const MaxColumns = 4;
var I, J, nJ: Integer;
  LastCells: Array[1..MaxColumns] of string;
  PrevItems: Array[1..MaxColumns] of TTreeNode;
  aNode: TTreeNode;
begin
  for I := 1 to MaxColumns do 
  begin
    LastCells[I] := '';
    PrevItems[I] := nil;
  end;
  aTreeview.Items.BeginUpdate;
  try
    for I := 1 to aGrid.Rows do 
    begin
      nJ := 0;
      for J := 1 to MaxColumns do  
        if aGrid.Cells[I, J] <> LastCells[J] then 
        begin
          nJ := J;
          Break;
        end;
      // nJ is the first column which is different
      // if nJ = 1 then first column = new top node
      if nJ = 1 then 
      begin
        aNode := aTreeView.Items.Add(nil, aGrid.Cells[I, nJ];
        // Reset prev subnodes
        for J := 2 to MaxColumns do 
          PrevItems[J] := nil;
      end 
        else
      begin
        aNode := aTreeView.Items.Add(PrevItems[nJ-1], aGrid.Cells[I, nJ]);
        // Reset prev subnodes
        for J := nJ + 1 to MaxColumns do 
          PrevItems[J] := nil;
      end;
      PrevItems[nj] := aNode;
      for J := 1 to MaxColumns do 
        LastCells[J] := aGrid.Cells[I, J];
    end;
  finally
    aTreeview.Items.EndUpdate;  
  end;
end;

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mikelittlewoodCommented:
As another alternate, you could take a look at the TMS TAdvStringGrid from www.tmssoftware.com
It has the ability of being able to group items like this automatically and build them as a treeview inside a the string grid. Much more flexible as you can turn on and off groupings of each individual column, allowing the user to view the data as they would like.
0
HenryM2Author Commented:
Sorry , I was under the impresion that the question was closed  
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.