Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

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

Posted on 2008-11-10
3
Medium Priority
?
353 Views
Last Modified: 2013-11-23
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.
0
Comment
Question by:HenryM2
3 Comments
 
LVL 38

Accepted Solution

by:
Geert Gruwez earned 1600 total points
ID: 22928552
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
 
LVL 15

Assisted Solution

by:mikelittlewood
mikelittlewood earned 400 total points
ID: 22930630
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
 

Author Closing Comment

by:HenryM2
ID: 31515240
Sorry , I was under the impresion that the question was closed  
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

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…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
The Relationships Diagram is a good way to get an overall view of what a database is keeping track of. It is also where relationships are defined. A relationship specifies how two tables connect to each other. As you build tables in Microsoft Ac…
Suggested Courses

577 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