首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > .NET >

delphi Treeview 3 层树 递归怎么做

2012-10-11 
delphi Treeview 3 层树 递归如何做?数据库中有数据表data_mx,其中表中数据如下:yearmonth day2007年10月2

delphi Treeview 3 层树 递归如何做?
数据库中有数据表data_mx,其中表中数据如下:
  year month day
  2007年 10月 20日
  2007年 10月 19日
  2007年 11月 10日
  2008年 8月 12日
  2010年 12月 17日
  2010年 5月 6日


想在窗口实现下列treeview显示:
|-2010年|-12月-17日
| |
| |-5月-6日
|
|
|
|-2008年|-8月-12日
|  
|
|-2007年|-11月|-10日
  |
  |-10月|-20日
  | |-19日
  |
注:无论年、月、日,都是递减显示,由于显示的原因,没有对齐,年和年,月和月,日和日上下都是对齐的。
该如何实现?谢谢

[解决办法]
先数据排序

当前年值:='';
当前月值:='';
当前年节点:=nil;
当前月节点:=nil;
再顺序处理每一条记录:
如年份字段值<>当前年值,则当前年节点:=treeview.items.add(nil,年份字段值);当前年值:=年份字段值;
如月份字段值<>当前月值,则当前月节点:=treeview.items.add(当前年节点,月份字段值);当前月值:=月份字段值;
treeview.items.add(当前月节点,日期字段值);
[解决办法]

Delphi(Pascal) code
unit Res_Department;interfaceuses  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,  Dialogs, Base_Unit, Buttons, ExtCtrls, ComCtrls, cxControls, cxContainer,  cxTreeView,CommDeclare, DB, ADODB, StdCtrls;type  TRes_Department_F = class(TBase_F)    cx_TV: TcxTreeView;    pnl_main: TPanel;    qry_tmp: TADOQuery;    qry_op: TADOQuery;    pnl_top: TPanel;    lbl1: TLabel;    edt_zg: TEdit;    lbl2: TLabel;    edt_jb: TEdit;    lbl3: TLabel;    edt_mail: TEdit;    procedure btnSB_SearchClick(Sender: TObject);    procedure cx_TVChange(Sender: TObject; Node: TTreeNode);    procedure FormCreate(Sender: TObject);    procedure cx_TVExpanded(Sender: TObject; Node: TTreeNode);    procedure cx_TVExpanding(Sender: TObject; Node: TTreeNode;      var AllowExpansion: Boolean);    procedure FormDestroy(Sender: TObject);  private    p_data:PString;    { Private declarations }  public    { Public declarations }  end;var  Res_Department_F: TRes_Department_F;implementationuses sys_datamoudle;{$R *.dfm}procedure TRes_Department_F.btnSB_SearchClick(Sender: TObject);var  iLoop:Integer;  Master,MasterNode:TTreeNode;begin  inherited;  qry_tmp.Close;  qry_tmp.SQL.Text:='select bmbh,bmmc from RES_DEPARTMENT where sjbm=''10000''';  qry_tmp.Open;  cx_TV.Items.BeginUpdate;  cx_TV.Items.Clear;  New(p_data);  p_data^:='10000';  Master:=cx_TV.Items.AddObject(nil,'總經理',p_data);  while not qry_tmp.Eof do  begin    Screen.Cursor:=crSQLWait;    New(p_data);    p_data^:=qry_tmp.fieldbyname('bmbh').AsString;    MasterNode:=cx_TV.Items.AddChildObject(Master,qry_tmp.fieldbyname('bmmc').AsString,p_data);    qry_op.Close;    qry_op.SQL.Text:='select bmbh,max(bmmc)as bmmc from RES_DEPARTMENT where sjbm='+ QuotedStr(qry_tmp.fieldbyname('bmbh').AsString)+' group by bmbh';    qry_op.Open;    Application.ProcessMessages;    for iLoop:=0 to qry_op.RecordCount -1 do    begin      New(p_data);      p_data^:=qry_op.fieldbyname('bmbh').AsString;      cx_TV.Items.AddChildObject(MasterNode,qry_op.fieldbyname('bmmc').AsString,p_data);      qry_op.Next;    end;    qry_tmp.Next;  end;  cx_TV.Items.EndUpdate;  Screen.Cursor:=crDefault;  Application.ProcessMessages;end;procedure TRes_Department_F.cx_TVChange(Sender: TObject; Node: TTreeNode);begin  inherited;  if Node.Data<>nil then  begin    qry_tmp.Close;    qry_tmp.SQL.Text:='select fzr,bmjb from RES_DEPARTMENT where bmbh='+ QuotedStr(pstring(Node.Data)^);    qry_tmp.Open;    edt_zg.Text:=qry_tmp.fieldbyname('fzr').AsString;    edt_jb.Text:=qry_tmp.fieldbyname('bmjb').AsString;  end;end;procedure TRes_Department_F.FormCreate(Sender: TObject);begin  inherited;  btnSB_SearchClick(Sender);end;procedure TRes_Department_F.cx_TVExpanded(Sender: TObject;  Node: TTreeNode);var  iLoop:Integer;begin  inherited;  if Node.Level>0 then  begin    for iLoop:=0 to Node.Count-1 do    begin      qry_op.Close;      qry_op.SQL.Text:='select top 1 bmbh from RES_DEPARTMENT where sjbm='+QuotedStr(pstring(Node.Item[iLoop].Data)^);      qry_op.Open;      if (qry_op.RecordCount>0) then        Node.Item[iLoop].HasChildren:=True;      Application.ProcessMessages;    end;  end;end;procedure TRes_Department_F.cx_TVExpanding(Sender: TObject;  Node: TTreeNode; var AllowExpansion: Boolean);begin  inherited;  if Node.Level>0 then  begin    Node.DeleteChildren;    qry_op.Close;    qry_op.SQL.Text:='select bmbh,max(bmmc)as bmmc from RES_DEPARTMENT where sjbm='+ QuotedStr(pstring(Node.Data)^)+' group by bmbh';    qry_op.Open;    while not qry_op.Eof do    begin      New(p_data);      p_data^:=qry_op.Fieldbyname('bmbh').AsString;      cx_TV.Items.AddChildObjectFirst(Node,qry_op.Fieldbyname('bmmc').AsString,p_data);      qry_op.Next;    end;  end;  end;procedure TRes_Department_F.FormDestroy(Sender: TObject);begin  inherited;  if p_data<>nil then    Dispose(p_data);end;end. 

热点排行