动手生成 Delphi xe DBTreeview

tProductType表结构如下

object FDConnection1: TFDConnection
    Params.Strings = (
      'Database=ClothingTem'
      'User_Name=sa'
      'Password=123'
      'Server=(local)'
      'DriverID=MSSQL')
    Connected = True
    LoginPrompt = False
    Left = 464
    Top = 288
  end
  object FDQ: TFDQuery
    Active = True
    Connection = FDConnection1
    SQL.Strings = (
      
        'select * from tProductType  where fParentID like '#39'0%'#39' order by f' +
        'code')
    Left = 464
    Top = 240
  end
  object DataSource1: TDataSource
    DataSet = FDQ
    Left = 320
    Top = 464
  end
end

代码如下

unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, FireDAC.Stan.Intf, FireDAC.Stan.Option,
  FireDAC.Stan.Error, FireDAC.UI.Intf, FireDAC.Phys.Intf, FireDAC.Stan.Def,
  FireDAC.Stan.Pool, FireDAC.Stan.Async, FireDAC.Phys, FireDAC.Phys.MSSQL,
  FireDAC.Phys.MSSQLDef, FireDAC.VCLUI.Wait, FireDAC.Stan.Param, FireDAC.DatS,
  FireDAC.DApt.Intf, FireDAC.DApt, Data.DB, Vcl.StdCtrls, Vcl.Grids,
  Vcl.DBGrids, Vcl.ComCtrls, FireDAC.Comp.DataSet, FireDAC.Comp.Client,
  Vcl.ExtCtrls;

type
  TForm1 = class(TForm)
    PageControl1: TPageControl;
    TabSheet1: TTabSheet;
    TabSheet2: TTabSheet;
    tv: TTreeView;
    FDConnection1: TFDConnection;
    FDQ: TFDQuery;
    DataSource1: TDataSource;
    Panel1: TPanel;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);

  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}


procedure TForm1.Button1Click(Sender: TObject);
var
nodes:ttreenodes;
node:ttreenode ;
begin
//     TV.Items.Clear;
//     nodes:=TV.Items;
//     FDQ.Active:=true;
//     node:=nodes.add(nil,FDQ.FieldByName('fCode').Value+'_'+FDQ.FieldByName('fNAME').Value);
//     FDQ.Next;
//     while NOT FDQ.Eof  do
//     BEGIN
//          if FDQ.FieldByName('fCode').Value<>0 then
//             nodes.add(node,FDQ.FieldByName('fCode').Value+'_'+FDQ.FieldByName('fNAME').Value);  //一级三兄弟
//             FDQ.Next;
//     END;
//     nodes.GetFirstNode
end;

procedure TForm1.Button2Click(Sender: TObject);
  var
    nodes:ttreenodes;
    node:ttreenode ;
    CurItem ,CurItem1:Ttreenode ;
    nodestr:string;
    lastparent,temp:string;
    i,j:integer;
begin

     TV.Items.Clear;
     nodes:=TV.Items;
     FDQ.Active:=true;
     node:=nodes.add(nil,FDQ.FieldByName('fCode').Value+'_'+FDQ.FieldByName('fNAME').Value);
     FDQ.Next;
     while NOT FDQ.Eof  do
     BEGIN
          if FDQ.FieldByName('fCode').Value<>0 then
             nodes.add(node,FDQ.FieldByName('fCode').Value+'_'+FDQ.FieldByName('fNAME').Value);  //一级三兄弟
             FDQ.Next;
     END;
     nodes.GetFirstNode;


   tv.Items.BeginUpdate;
  for I := 1 to 9 do
    begin
      FDQ.SQL.Clear;
      temp:=inttostr(i);
      fdq.SQL.Text:='select * from tProductType  where fcode like '+''''+ temp+'%'  +'''';//+' order by fParentCode';
      fdq.Prepare;
      fdq.open;
      nodes:=Tv.Items;
      CurItem := TV.Items.GetFirstNode;
      if i>1 then
          for j := 1 to i-1 do
          begin
           CurItem1:=CurItem.getNextSibling;
           CurItem:= CurItem1;
          end;
      fdq.First;
      fdq.Next;
      lastparent:=fdq.FieldByName('fParentCode').AsString;
      while not fdq.Eof  do
         begin
            //如 果当前'fParentCode'与上次不一样就查找
          if  lastparent<>fdq.FieldByName('fParentCode').AsString
            then
             begin
               CurItem := TV.Items.GetFirstNode;
                while CurItem <> nil do
                   begin
                    if copy( CurItem.Text,1,pos('_',CurItem.Text)-1) =fdq.FieldByName('fParentCode').Value then
                       begin
                        CurItem.Selected:=true;
                        break;
                        end;
                       CurItem := CurItem.GetNext;
                   end;
             end ;
             begin
                 nodes.addchild(CurItem,FDQ.FieldByName('fCode').Value+'_'+FDQ.FieldByName('fNAME').Value);
                 lastparent:=fdq.FieldByName('fParentCode').AsString;
             end;
          fdq.Next;
         end;
  end; // for I := 0 to 9 do
   tv.Items.EndUpdate;
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
//showmessage(tv.Items.Count.ToString );
end;

end.

运行结果如下图

猜你喜欢

转载自www.cnblogs.com/jijm123/p/11336346.html