Thema: Delphi Treeview with databse

Einzelnen Beitrag anzeigen

marabu

Registriert seit: 6. Apr 2005
10.109 Beiträge
 
#2

Re: Treeview with databse

  Alt 15. Jul 2007, 20:24
Hi,

since you end up mixing names and values in one single node.text you should not be very happy with a TreeView. Just to show a technical solution I will provide some code:

Delphi-Quellcode:
type
  TDemoForm = class(TForm)
    TreeView: TTreeView;
    Table: TTable;
    Button: TButton;
    procedure ButtonClick(Sender: TObject);
  private
    procedure AddInfo(ds: TDataSet; node: TTreeNode; names: array of string);
  end;

var
  DemoForm: TDemoForm;

implementation

{$R *.dfm}

procedure TDemoForm.AddInfo(ds: TDataSet; node: TTreeNode; names: array of string);
var
  i: Integer;
begin
  with ds, node do
    for i := Low(names) to High(names) do
      node.Owner.AddChild(node, FieldByName(names[i]).AsString);
end;

procedure TDemoForm.ButtonClick(Sender: TObject);
var
  bm: TBookmark;
begin
  TreeView.Items.Clear;
  with Table do
  begin
    bm := GetBookmark;
    DisableControls;
    First;
    while not Eof do
    begin
      AddInfo(Table,
        TreeView.Items.AddChildObject(nil, 'Contact', Pointer(RecNo)),
        ['NAME', 'FIRST_NAME']
      );
      Next;
    end;
    GotoBookmark(bm);
    FreeBookmark(bm);
    EnableControls;
  end;
end;
This code is not tested, but should show some basic principles. Think twice about the TreeView, though.

Regards
  Mit Zitat antworten Zitat