Q&A

  • TreeView에 Node추가하는거 다시 도와주세요..T.T
DB에 자료가 3개의 필드로 들어가있어요..

01 - 0 - 00
01 - 1 - 00
01 - 1 - 01
01 - 1 - 02
01 - 2 - 00
01 - 2 - 01
02 - 0 - 00
02 - 1 - 00
...

이것을 Record의 포인터로 설정후 그것을 AddObject또는
AddChildObject하고 싶은데요..

TreeView에
------------
01                     -->   01 - 0 - 00
    1                  -->   01 - 1 - 00
        1               -->  01 - 1 - 01
        2               -->  01 - 1 - 02
    2                   -->  01 - 2 - 00
        1               -->  01 - 2 - 01
02                     -->  02 - 0 - 00
    1                   -->  02 - 1 - 00
------------
이렇게 나타내고 싶은데요..
쿼리문 날려서 not eof될때까지...해서
01 - 1 - 02 을 추가한후 01 - 2 - 00을 넣을려고
상위 Node로 TreeView.Selected를 올리고 싶거든요..
어떻게 하면 되는지 ㅋㅋ
한수 부탁드립니다.....^^?

코드는 아래와 같이 구현중입니다....
===================================
procedure TForm1.Button1Click(Sender: TObject);
var
   sSQL, sGuCD, sGubun, sCode : String;
   TND : TTreeNode;
begin
   sSQL := 'select * from sfm002f ' +
           'where GbCD < ''80'' ' +
           'order by GbCD, Gubun, Code ';
   with Query1, TreeView1 do begin
      sql.Text := sSQL;
      Open;
      First;
      while not eof do begin
         New(Wichi);
         Wichi^.GbCd := Fields[0].AsString;
         Wichi^.Gubun := Fields[1].AsString;
         Wichi^.Code := Fields[2].AsString;
         Wichi^.Name := Fields[3].AsString;

         //CreateTreeNode(TreeView1, Wichi);

         sGuCD := Wichi^.GbCd;
         sGubun := Wichi^.Gubun;
         sCode := Wichi^.Code;

         Next;
      end;
   end;
end;
===================================
1  COMMENTS
  • Profile
    전문성 2003.03.08 08:19
    unit Unit1;

    interface

    uses
      Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
      Db, DBTables, StdCtrls, Buttons, ComCtrls;

    type
      RWichi = record
        gbcd, gubun, code, name : String;
      end;
      pWichi = ^RWichi;

      TForm1 = class(TForm)
        TreeView1: TTreeView;
        BitBtn1: TBitBtn;
        Database1: TDatabase;
        Query1: TQuery;
        Label1: TLabel;
        procedure BitBtn1Click(Sender: TObject);
        procedure FormClose(Sender: TObject; var Action: TCloseAction);
        procedure TreeView1Change(Sender: TObject; Node: TTreeNode);
      private
        { Private declarations }
      public
        { Public declarations }
      end;

    var
      Form1: TForm1;
      Wichi : pWichi;

    implementation

    {$R *.DFM}

    procedure SetSelect(var TV: TTreeView; WC: pWichi);
    var
       i : Integer;
       Node : TTreeNode;
       sTemp : String;
    begin
       for i := 0 to TV.Items.Count - 1 do begin
          Node := TV.Items.Item[i];
          Node.getFirstChild;
          //sTemp := pWichi(Node.Data)^.name;
          //showmessage('sTemp = ' + sTemp);
          if pWichi(Node.Data)^.gbcd <> WC.gbcd then TV.Selected := Node;
       end;
    end;

    procedure TForm1.BitBtn1Click(Sender: TObject);
    var
       sSQL, sGbCD, sGubun, sCode : String;
       Node1, Node2 : TTreeNode;
    begin
       sSQL := 'select * from Code ' +
               'order by GbCD, Gubun, Code ';
       with Query1, TreeView1 do begin
          sql.Text := sSQL;
          Open;      First;
          Items.Clear;
          Node1 := Selected;
          while not eof do begin
             New(Wichi);
             Wichi^.gbcd := Fields[0].AsString;
             Wichi^.gubun := Fields[1].AsString;
             Wichi^.code := Fields[2].AsString;
             Wichi^.name := Fields[3].AsString;

             if ((Node1 = nil)or(Wichi^.gbcd <> pWichi(Node1.Data).gbcd)) then begin
                Node1 := Items.AddObject(Node1, Wichi^.name, Wichi);
                Node2 := Node1.GetLastChild;
             end
             else begin
                if Wichi^.gubun <> pWichi(Node1.Data).gubun then begin
                   if ((Node2 = nil)or(Wichi^.gubun <> pWichi(Node2.Data).gubun)) then begin
                      Node2 := Items.AddChildObject(Node1, Wichi^.name, Wichi);
                   end
                   else begin
                      if Wichi^.code <> pWichi(Node2.Data).code then
                         Items.AddChildObject(Node2, Wichi^.name, Wichi);
                   end;
                end;
             end;

             Next;
          end;
       end;
    end;

    procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
    var
       i : Integer;
       TND : TTreeNode;
    begin
       with TreeView1 do begin
          for i := 0 to Items.Count - 1 do begin
             if Items[i].Level > 0 then
                Dispose(pWichi(Items[i].Data));
          end;
       end;
    end;

    procedure TForm1.TreeView1Change(Sender: TObject; Node: TTreeNode);
    var
       sTemp : String;
    begin
       sTemp := pWichi(Node.Data).gbcd + ' - ' + pWichi(Node.Data).gubun + ' - ' +
                pWichi(Node.Data).code + ' - ' + pWichi(Node.Data).name;
       Label1.Caption := sTemp;
    end;

    end.
    ====================> 이렇게 하니까 되네요...아이 기쁘다..감사합니다!