unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComCtrls, Grids, DBGrids,
DB, StdCtrls, Buttons, ADODB;
type
TMyHardware =
class(TForm)
ADOQuery1: TADOQuery;
ADODataSet1: TADODataSet;
ADOConnection1: TADOConnection;
DataSource1: TDataSource;
DBGrid1: TDBGrid;
TreeView1: TTreeView;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private-Deklarationen }
public
{ Public-Deklarationen }
end;
var
MyHardware: TMyHardware;
procedure FillTreeView(TheView : TTreeView; TheData : TDataSet);
implementation
{$R *.dfm}
procedure FillTreeView(TheView : TTreeView; TheData : TDataSet);
type
TMyHardware =
Record
Id : Integer;
IdParent : Integer;
end;
PMyHardware = ^TMyHardware;
// diese Prozedur verschiebt den übergebenen Knoten and
// die richtige Position im TreeView
procedure MoveToParentNode(TheNode: TTreeNode);
var x : Integer;
begin
for x := 0
to TheView.Items.Count-1
do begin
if Assigned(TheView.Items[x].Data)
then begin
if PMyHardware(TheView.Items[x].Data).Id = PMyHardware(TheNode.Data).IdParent
then begin
TheNode.MoveTo(TheView.Items[x],naAddChild);
Break;
end;
end;
end;
end;
// Beginn von FillTreeView:
var Root,AktNode : TTreeNode;
i : Integer;
begin
TheData.First;
Root := TTreeNode.Create(TheView.Items);
Root.Text := '
Hardware';
Root.Data :=
nil;
while not TheData.EOF
do begin
AktNode := TTreeNode.Create(TheView.Items);
AktNode.Data := new(PMyHardware);
PMyHardware(AktNode.Data).Id := TheData.FieldByName('
IntArtKatId').AsInteger;
PMyHardware(AktNode.Data).IdParent := TheData.FieldByName('
IntArtKatParentID').AsInteger;
TheData.Next;
end;
for i := 0
to TheView.Items.Count-1
do begin
if Assigned(TheView.Items[i].Data)
then begin
if PMyHardware(TheView.Items[i].Data).IdParent <> 0
then MoveToParentNode(TheView.Items[i]);
end;
end;
end;
procedure TMyHardware.Button1Click(Sender: TObject);
begin
FillTreeView(TreeView1,ADOQuery1);
end;
end.