动手生成 Delphi xe DBTreeview

31 篇文章 0 订阅
15 篇文章 1 订阅

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.

运行结果如下图

 

 

---------------------------------------------------------------------------------------------------------------------------------------------------

以下用 插件DevExpress VCL_18.2.5  TdxDBTreeView   +delphi 10.3 

 设置如下object Form5: TForm5
  Left = 0
  Top = 0
  Caption = 'Form5'
  ClientHeight = 300
  ClientWidth = 635
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object dxDBTreeView1: TdxDBTreeView
    Left = 24
    Top = 40
    Width = 201
    Height = 228
    ShowNodeHint = True
    DataSource = DataSource1
    KeyField = 'fCode'
    ListField = 'fName'
    ParentField = 'fParentCode'

    RootValue = Null
    SeparatedSt = ' - '
    RaiseOnError = True
    Indent = 19
    ParentColor = False
    Options = [trDBCanDelete, trDBConfirmDelete, trCanDBNavigate, trSmartRecordCopy, trCheckHasChildren]
    SelectedIndex = -1
    TabOrder = 0
  end
  object DBNavigator1: TDBNavigator
    Left = 0
    Top = 0
    Width = 635
    Height = 25
    DataSource = DataSource1
    Align = alTop
    TabOrder = 1
  end
  object ClothuConnection: TFDConnection
    Params.Strings = (
      'ConnectionDef=ClothU')
    Connected = True
    LoginPrompt = False
    Left = 383
    Top = 87
  end
  object TareaTable: TFDQuery
    Active = True
    Connection = ClothuConnection
    SQL.Strings = (
      'SELECT * FROM ClothU.dbo.tArea')
    Left = 383
    Top = 135
  end
  object DataSource1: TDataSource
    DataSet = TareaTable
    Left = 312
    Top = 152
  end
end

 

 

 

 

delphi,学pascal那么久了,很多知识也都是互联网上搜索,也是依靠很多网友提供的资料去学习的,在此感谢互联网, 感谢那些技术分享的朋友们,近来打算在鄙人这年纪最后完成一个二次开发框架的软件(全凭自己的兴趣,因本人就一IT宅男,搞完后 估计本人也没有精力学习其它了),此软件想尽量少用第三方控件,但里面的DBTree功能不可避免,本以为网上找个DBTree是个手到擒来 的事情,没想到百度了一整天,居然没有一个好的DBTree,也许有人会说Ehlib的dbgridEH+Memtableeh可以,但这第三方软件包还需要带 有其它的东西,有点大,前面说过了,本软件想尽量少用第三方,除此外,国内的DBTree控件居然没有几个行的,看来delphi真的落寞了, 只能祝福delphixe5跨平台再次崛起吧!其实也不是说国产的DBTree没有,有那么两三个人做过,但我测试后很多问题,有些还把SQL做 到控件里,而且关联死TADOQuery,这样的控件让人以后如何用,国外的DBTree目前就发现一个还可以的是Dxdbtree, 公司也是大名顶顶的Developer Express Inc ,但不知道为何,网上找的只有1.3.1版本,而且只是For D7的,装上后, 发现还是要引用自己的内存数据(MemData)表才行,看了下源码,写得很复杂,晕啊,我只想找个简简单单功能的DBTree控件,怎么那么难呢, 要求也不高,只需要继承于TTreeView,载入数据表结构,展示出一个TreeView,能简单的拖动,删除结构的操作就行, 求人不如求自,既然简单的就自己来做一个控件吧!经过三天参看源码和网络技术资料搜索,基本学会了delphi控件开发了, 因鄙人之前并没有真正做过什么控件,最后做出来的结果感觉还是不错的,鄙人在此公布源码并对源码关键地方用中文详细注释, 目的是希望给以后有类似像我这样的找DBTree的朋友一点帮助!同时也是给初学delphi控件编程的朋友是一个好教材!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值