参考
procedure Ttdeform.FormCreate(Sender: TObject);
var m,n,k:integer;
gcm,zm:string;
begin with rationdatamodule.demlquery do
begin
close;
sql.Clear;
sql.Add('select distinct gcm,gcbh from t_deml order by gcbh');
open;
if recordcount>0 then
begin
for m:=0 to recordcount-1 do
begin
gcm:=fieldbyname('gcm').asstring;
treeNode0:=TreeView1.Items.Add(nil,fieldbyname('gcm').asstring);
treeNode0.ImageIndex:=0;
with rationdatamodule.demladoquery1 do
begin
close;
sql.clear;
sql.Add('select distinct zm,zh from t_deml where gcm=:p_gcm order by zh');
parameters.ParamByName('p_gcm').value:=gcm;
open;
if recordcount>0 then
begin
for n:=0 to recordcount-1 do
begin
zm:=fieldbyname('zm').asstring;
treeNode1:=TreeView1.Items.Addchild(treeNode0,fieldbyname('zh').asstring+':'+fieldbyname('zm').asstring);
treeNode1.ImageIndex:=1;
with rationdatamodule.demladoquery2 do
begin
close;
sql.Clear;
sql.add('select * from t_deml where zm=:p_zm');
parameters.ParamByName('p_zm').value:=zm;
open;
if recordcount>0 then
begin
for k:=0 to recordcount-1 do
begin
treeNode2:=TreeView1.Items.Addchild(treeNode1,fieldbyname('zjbm').asstring+':'+fieldbyname('xjmc').asstring);
treeNode2.ImageIndex:=2;
next;
end;
end;
end;
next;
end;
end;
end;
next;
end;
end;
end;
procedure Ttdeform.FormCreate(Sender: TObject);
var m,n,k:integer;
gcm,zm:string;
begin with rationdatamodule.demlquery do
begin
close;
sql.Clear;
sql.Add('select distinct gcm,gcbh from t_deml order by gcbh');
open;
if recordcount>0 then
begin
for m:=0 to recordcount-1 do
begin
gcm:=fieldbyname('gcm').asstring;
treeNode0:=TreeView1.Items.Add(nil,fieldbyname('gcm').asstring);
treeNode0.ImageIndex:=0;
with rationdatamodule.demladoquery1 do
begin
close;
sql.clear;
sql.Add('select distinct zm,zh from t_deml where gcm=:p_gcm order by zh');
parameters.ParamByName('p_gcm').value:=gcm;
open;
if recordcount>0 then
begin
for n:=0 to recordcount-1 do
begin
zm:=fieldbyname('zm').asstring;
treeNode1:=TreeView1.Items.Addchild(treeNode0,fieldbyname('zh').asstring+':'+fieldbyname('zm').asstring);
treeNode1.ImageIndex:=1;
with rationdatamodule.demladoquery2 do
begin
close;
sql.Clear;
sql.add('select * from t_deml where zm=:p_zm');
parameters.ParamByName('p_zm').value:=zm;
open;
if recordcount>0 then
begin
for k:=0 to recordcount-1 do
begin
treeNode2:=TreeView1.Items.Addchild(treeNode1,fieldbyname('zjbm').asstring+':'+fieldbyname('xjmc').asstring);
treeNode2.ImageIndex:=2;
next;
end;
end;
end;
next;
end;
end;
end;
next;
end;
end;
end;
unit DBTreeView;interfaceuses
Variants, Windows, SysUtils, Messages, Controls, Forms, Classes, ComCtrls,
Graphics, Menus, StdCtrls, ExtCtrls, Mask, Buttons, DB, DBCtrls, CommCtrl;type
TDBTreeView = class(TCustomTreeView)
private
FDataLink: TFieldDataLink;
FCanvas: TControlCanvas;
FFocused: Boolean;
procedure ActiveChange(Sender: TObject);
procedure DataChange(Sender: TObject);
procedure EditingChange(Sender: TObject);
function GetDataField: string;
function GetDataSource: TDataSource;
function GetField: TField;
function GetReadOnly: Boolean;
procedure SetDataField(const Value: string);
procedure SetDataSource(Value: TDataSource);
procedure SetFocused(Value: Boolean);
procedure SetReadOnly(Value: Boolean);
procedure UpdateData(Sender: TObject);
procedure CMEnter(var Message: TCMEnter); message CM_ENTER;
procedure CMExit(var Message: TCMExit); message CM_EXIT;
procedure CMGetDataLink(var Message: TMessage); message CM_GETDATALINK;
protected
procedure KeyDown(var Key: Word; Shift: TShiftState); override;
procedure KeyPress(var Key: Char); override;
procedure Loaded; override;
procedure Notification(AComponent: TComponent;
Operation: TOperation); override;
procedure Edit(const Item: TTVItem); override;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
function ExecuteAction(Action: TBasicAction): Boolean; override;
function UpdateAction(Action: TBasicAction): Boolean; override;
property Field: TField read GetField;
published
property DataField: string read GetDataField write SetDataField;
property DataSource: TDataSource read GetDataSource write SetDataSource;
property ReadOnly: Boolean read GetReadOnly write SetReadOnly default False;
property Align;
property Anchors;
property AutoExpand;
property BevelEdges;
property BevelInner;
property BevelOuter;
property BevelKind default bkNone;
property BevelWidth;
property BiDiMode;
property BorderStyle;
property BorderWidth;
property ChangeDelay;
property Color;
property Ctl3D;
property Constraints;
property DragKind;
property DragCursor;
property DragMode;
property Enabled;
property Font;
property HideSelection;
property HotTrack;
property Images;
property Indent;
property MultiSelect;
property MultiSelectStyle;
property ParentBiDiMode;
property ParentColor default False;
property ParentCtl3D;
property ParentFont;
property ParentShowHint;
property PopupMenu;
property RightClickSelect;
property RowSelect;
property ShowButtons;
property ShowHint;
property ShowLines;
property ShowRoot;
property SortType;
property StateImages;
property TabOrder;
property TabStop default True;
property ToolTips;
property Visible;
property OnAddition;
property OnAdvancedCustomDraw;
property OnAdvancedCustomDrawItem;
property OnChange;
property OnChanging;
property OnClick;
property OnCollapsed;
property OnCollapsing;
property OnCompare;
property OnContextPopup;
property OnCreateNodeClass;
property OnCustomDraw;
property OnCustomDrawItem;
property OnDblClick;
property OnDeletion;
property OnDragDrop;
property OnDragOver;
property OnEdited;
property OnEditing;
property OnEndDock;
property OnEndDrag;
property OnEnter;
property OnExit;
property OnExpanding;
property OnExpanded;
property OnGetImageIndex;
property OnGetSelectedIndex;
property OnKeyDown;
property OnKeyPress;
property OnKeyUp;
property OnMouseDown;
property OnMouseMove;
property OnMouseUp;
property OnStartDock;
property OnStartDrag;
{ Items must be published after OnGetImageIndex and OnGetSelectedIndex }
property Items;
end;procedure Register;implementationprocedure Register;
begin
RegisterComponents('Zsoft', [TDBTreeView]);
end;{ TDBTreeView }procedure TDBTreeView.ActiveChange(Sender: TObject);
begin
//
end;procedure TDBTreeView.CMEnter(var Message: TCMEnter);
begin
SetFocused(True);
inherited;
if SysLocale.FarEast and FDataLink.CanModify then
inherited ReadOnly := False;
end;procedure TDBTreeView.CMExit(var Message: TCMExit);
begin
try
FDataLink.UpdateRecord;
except
SetFocus;
raise;
end;
SetFocused(False);
DoExit;
end;procedure TDBTreeView.CMGetDataLink(var Message: TMessage);
begin
Message.Result := Integer(FDataLink);
end;constructor TDBTreeView.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
inherited ReadOnly := True;
ControlStyle := ControlStyle + [csReplicatable];
FDataLink := TFieldDataLink.Create;
FDataLink.Control := Self;
FDataLink.OnDataChange := DataChange;
FDataLink.OnEditingChange := EditingChange;
FDataLink.OnUpdateData := UpdateData;
FDataLink.OnActiveChange := ActiveChange;
end;procedure TDBTreeView.DataChange(Sender: TObject);
var
vStringStream: TStringStream;
begin
if FDataLink.Field <> nil then begin
vStringStream := TStringStream.Create(FDataLink.Field.AsString);
try
vStringStream.Position := 0;
LoadFromStream(vStringStream);
finally
vStringStream.Free;
end;
end;
end;destructor TDBTreeView.Destroy;
begin
FDataLink.Free;
FDataLink := nil;
FCanvas.Free;
inherited Destroy;
end;procedure TDBTreeView.Edit(const Item: TTVItem);
begin
FDataLink.Modified;
inherited Edit(Item);
end;procedure TDBTreeView.EditingChange(Sender: TObject);
begin
inherited ReadOnly := not FDataLink.Editing;
end;function TDBTreeView.ExecuteAction(Action: TBasicAction): Boolean;
begin
Result := inherited ExecuteAction(Action) or (FDataLink <> nil) and
FDataLink.ExecuteAction(Action);
end;function TDBTreeView.GetDataField: string;
begin
Result := FDataLink.FieldName;
end;function TDBTreeView.GetDataSource: TDataSource;
begin
Result := FDataLink.DataSource;
end;function TDBTreeView.GetField: TField;
begin
Result := FDataLink.Field;
end;function TDBTreeView.GetReadOnly: Boolean;
begin
Result := FDataLink.ReadOnly;
end;procedure TDBTreeView.KeyDown(var Key: Word; Shift: TShiftState);
begin
inherited KeyDown(Key, Shift);
if (Key = VK_DELETE) or ((Key = VK_INSERT) and (ssShift in Shift)) then
FDataLink.Edit;
end;procedure TDBTreeView.KeyPress(var Key: Char);
begin
inherited KeyPress(Key);
if (Key in [#32..#255]) and (FDataLink.Field <> nil) and
not FDataLink.Field.IsValidChar(Key) then
begin
MessageBeep(0);
Key := #0;
end;
case Key of
^H, ^V, ^X, #32..#255:
FDataLink.Edit;
#27:
begin
FDataLink.Reset;
Key := #0;
end;
end;
end;procedure TDBTreeView.Loaded;
begin
inherited Loaded;
if (csDesigning in ComponentState) then DataChange(Self);
end;procedure TDBTreeView.Notification(AComponent: TComponent;
Operation: TOperation);
begin
inherited Notification(AComponent, Operation);
if (Operation = opRemove) and (FDataLink <> nil) and
(AComponent = DataSource) then DataSource := nil;
end;procedure TDBTreeView.SetDataField(const Value: string);
begin
FDataLink.FieldName := Value;
end;procedure TDBTreeView.SetDataSource(Value: TDataSource);
begin
if not (FDataLink.DataSourceFixed and (csLoading in ComponentState)) then
FDataLink.DataSource := Value;
if Value <> nil then Value.FreeNotification(Self);
end;procedure TDBTreeView.SetFocused(Value: Boolean);
begin
if FFocused <> Value then
begin
FFocused := Value;
FDataLink.Reset;
end;
end;procedure TDBTreeView.SetReadOnly(Value: Boolean);
begin
FDataLink.ReadOnly := Value;
inherited ReadOnly := Value;
end;function TDBTreeView.UpdateAction(Action: TBasicAction): Boolean;
begin
Result := inherited UpdateAction(Action) or (FDataLink <> nil) and
FDataLink.UpdateAction(Action);
end;procedure TDBTreeView.UpdateData(Sender: TObject);
var
vStringStream: TStringStream;
begin
if FDataLink.Field <> nil then begin
vStringStream := TStringStream.Create('');
try
SaveToStream(vStringStream);
vStringStream.Position := 0;
FDataLink.Field.AsString := vStringStream.DataString
finally
vStringStream.Free;
end;
end;
end;end.
你的DbTreeView怎么用啊。
我用Tclientdataset1组件-->DataSource1 并把数据源acitve:=true;
最后连你的dbtreeview的DataSource=DataSource1
连datafield='levels'
我的表中levels已有数据。
----------
levels ...
-----------------
001
001.001
001.001.01
-----------------
发现你的树只显示一个节点,取当前记录值。
?
请指教。