dbtreeview(100分)

  • 主题发起人 主题发起人 cui guohai
  • 开始时间 开始时间
C

cui guohai

Unregistered / Unconfirmed
GUEST, unregistred user!
朋友,我不知道如何使用delphi中的TREEVIEW与数据表绑定,请指教!!!
 
没有DELPHI控件,自己做一个也行,伙用第三方控件
 
把这些代码拷贝起来编译成一个DBTreeView吧。可以的话就给我加分。
unit DBTrees;

interface

uses ComCtrls,DB,Classes;

type

TDBTreeView=class(TTreeView)
private
fTable:TDataSet;
fId,fParentId,fName:string;
function AddItem:TTreeNode;
public
function GetId:integer;
procedure BuildTree;
procedure ClearTree;
function FindItem(Id:integer):TTreeNode;
function SelectItem(Id:integer):TTreeNode;
published
property FieldID:string Read fID Write fID;
property FieldParentID:string Read fParentId Write fParentId;
property FieldName:string Read fName Write fName;
property DataSource:TDataSet Read fTable Write fTable;
property ActiveId:integer Read GetId;
end;

procedure Register;

implementation

function TDBTreeView.FindItem(Id:integer):TTreeNode;
var
i:integer;
begin
Result:=nil;
For i:=0 to Items.Count-1 do
begin
if integer(Items.Data) = Id then
begin
Result:=Items;
Exit;
end;
end;
end;

function TDBTreeView.GetId:integer;
begin
If Selected=nil then Result:= -1
else Result:=integer(Selected.Data);
end;

function TDBTreeView.AddItem:TTreeNode;
var
ObjParent:TTreeNode;
Name:string;
CurId:integer;
ParentID:integer;
begin
ObjParent:=FindItem(fTable.FieldByName(fId).AsInteger);
If ObjParent<>nil then
begin
Result:=ObjParent;
Exit;
end;

Name:=fTable.FieldByName(fName).AsString;
CurId :=fTable.FieldByName(fID).Asinteger;

If fTable.FieldByName(fParentId).AsString<>'' then
begin
ParentID := fTable.FieldByName(fParentId).AsInteger;
ObjParent:=FindItem(ParentId);
If ObjParent<>nil then
Result:=Items.AddChildObject(ObjParent,Name,Pointer(CurID))
else
begin
if fTable.Locate(fID,ParentID,[]) then
Result := Items.AddChildObject(AddItem,Name,Pointer(CurID))
else
Result := Items.AddObject(nil,name,Pointer(CurID));
end;
end
else
Result:=Items.AddObject(nil,Name,Pointer(CurID));
end;

procedure TDBTreeView.BuildTree;
var ChangeEvent:TTVChangedEvent;
begin
If (fTable=nil) or (not fTable.Active) then Exit;
ChangeEvent:=OnChange;
OnChange:=nil;
Items.BeginUpdate;
ClearTree;
fTable.First;
While not fTable.Eof do
begin
AddItem;
fTable.Next;
end;
Items.EndUpdate;
OnChange:=ChangeEvent;
end;

procedure TDBTreeView.ClearTree;
begin
Items.BeginUpdate;
While Items.Count>0 do Items.Delete(Items[0]);
Items.EndUpdate;
end;

procedure Register;
begin
RegisterComponents('Data Controls',[TDBTreeView]);
end;

function TDBTreeView.SelectItem(Id: integer): TTreeNode;
var
aNode:TTreeNode;
begin
result := FindItem(Id);
if result = nil then exit;
aNode := result.parent;
while aNode <> nil do
begin
aNode.Expand(false);
aNode := aNode.Parent;
end;
result.Selected := true;
end;

end.
 
接受答案了.
 
后退
顶部