跪求用delphi做treeview连接数据库 并点击节点 相关数据读到dbgrid中去(QQ邮箱:188021494@qq.com)
1个回答
展开全部
给你个例子
unit PublicTree;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ComCtrls, ImgList,DB, Menus;
type
TFrameTree = class(TFrame)
tvTree: TTreeView;
ImageList1: TImageList;
procedure tvTreeExpanding(Sender: TObject; Node: TTreeNode;
var AllowExpansion: Boolean);
procedure tvTreeChange(Sender: TObject; Node: TTreeNode);
private
PDataSet:TDataSet;
PID,PName,PParentId:TField;
{ Private declarations }
public
procedure TreeInit(DataSet:TDataSet;ID,Name,ParentId:TField);
function TreeAddItem(Parent: TTreeNode;Resort: Boolean): TTreeNode;
procedure TreeDeleteItem(node:TTreeNode);
function TreeFindItem(ID:string): TTreeNode;
{ Public declarations }
end;
implementation
uses PubFun;
{$R *.dfm}
{ TFrameTree }
function TFrameTree.TreeAddItem(Parent: TTreeNode;Resort: Boolean): TTreeNode;
var
ParentNo: string;
node: TTreeNode;
begin
if (parent=nil) or (pos('_',parent.text)<1) then
ParentNo:=''
else
ParentNo:=GetCommaStr(Parent.Text,1,'_');
Pdataset.DisableControls;
Pdataset.First;
while not pdataset.Eof do
begin
if PParentId.AsString= ParentNo then
begin
node:=tvTree.Items.AddChild(parent,pid.asstring+'_'+pname.asstring);
node.HasChildren:=true;
node.ImageIndex:=0;
node.SelectedIndex:=1;
end;
pdataset.Next;
end;
Pdataset.EnableControls;
if Resort and (parent<>nil) then parent.alphasort;
if(Parent<>nil)and(Parent.Count<=0)then Parent.HasChildren:=false;
if tvTree.Selected=nil then
begin
node:=treefinditem(pid.AsString);
tvTree.Selected:=node;
end
else
Pdataset.Locate(pid.FieldName,GetCommaStr(tvTree.Selected.text,1,'_'),[]);
end;
procedure TFrameTree.TreeInit(DataSet:TDataSet;ID,Name,ParentId:TField);
begin
PDataset:=Dataset;
PID:=ID;
PName:=name;
PParentID:=parentID;
end;
procedure TFrameTree.tvTreeExpanding(Sender: TObject; Node: TTreeNode;
var AllowExpansion: Boolean);
begin
if node.Count<=0 then
treeadditem(node,true);
end;
procedure TFrameTree.tvTreeChange(Sender: TObject; Node: TTreeNode);
begin
if pdataset.State in [dsInsert,dsEdit] then pDataset.Post;
if node<>nil then
if pos('_',node.Text)>0 then
if not Pdataset.Locate(PID.FieldName,GetCommaStr(node.text,1,'_'),[]) then
raise exception.Create('记录未找到!');
end;
procedure TFrameTree.TreeDeleteItem(node: TTreeNode);
begin
tvTree.Items.Delete(node);
end;
function TFrameTree.TreeFindItem(ID:string): TTreeNode;
var
node:TTreeNode;
begin
Node:= tvTree.Items.GetFirstNode;
if (Node<> nil) and (GetCommaStr(Node.text,1,'_') <> ID) then
repeat
Node := Node.GetNext;
until (Node = nil) or (GetCommaStr(Node.text,1,'_')= ID);
Result := Node;
end;
end.
调用生成
cdsTree.Open;
FrameTree1.TreeInit(cdsTree,cdsTree.FieldByName(CodeField),cdsTree.FieldByName(NameField),cdsTree.FieldByName(ParentField));
node:=FrameTree1.tvTree.Items.Add(nil,TopText);
node.HasChildren:=true;
node.Expand(false);
unit PublicTree;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ComCtrls, ImgList,DB, Menus;
type
TFrameTree = class(TFrame)
tvTree: TTreeView;
ImageList1: TImageList;
procedure tvTreeExpanding(Sender: TObject; Node: TTreeNode;
var AllowExpansion: Boolean);
procedure tvTreeChange(Sender: TObject; Node: TTreeNode);
private
PDataSet:TDataSet;
PID,PName,PParentId:TField;
{ Private declarations }
public
procedure TreeInit(DataSet:TDataSet;ID,Name,ParentId:TField);
function TreeAddItem(Parent: TTreeNode;Resort: Boolean): TTreeNode;
procedure TreeDeleteItem(node:TTreeNode);
function TreeFindItem(ID:string): TTreeNode;
{ Public declarations }
end;
implementation
uses PubFun;
{$R *.dfm}
{ TFrameTree }
function TFrameTree.TreeAddItem(Parent: TTreeNode;Resort: Boolean): TTreeNode;
var
ParentNo: string;
node: TTreeNode;
begin
if (parent=nil) or (pos('_',parent.text)<1) then
ParentNo:=''
else
ParentNo:=GetCommaStr(Parent.Text,1,'_');
Pdataset.DisableControls;
Pdataset.First;
while not pdataset.Eof do
begin
if PParentId.AsString= ParentNo then
begin
node:=tvTree.Items.AddChild(parent,pid.asstring+'_'+pname.asstring);
node.HasChildren:=true;
node.ImageIndex:=0;
node.SelectedIndex:=1;
end;
pdataset.Next;
end;
Pdataset.EnableControls;
if Resort and (parent<>nil) then parent.alphasort;
if(Parent<>nil)and(Parent.Count<=0)then Parent.HasChildren:=false;
if tvTree.Selected=nil then
begin
node:=treefinditem(pid.AsString);
tvTree.Selected:=node;
end
else
Pdataset.Locate(pid.FieldName,GetCommaStr(tvTree.Selected.text,1,'_'),[]);
end;
procedure TFrameTree.TreeInit(DataSet:TDataSet;ID,Name,ParentId:TField);
begin
PDataset:=Dataset;
PID:=ID;
PName:=name;
PParentID:=parentID;
end;
procedure TFrameTree.tvTreeExpanding(Sender: TObject; Node: TTreeNode;
var AllowExpansion: Boolean);
begin
if node.Count<=0 then
treeadditem(node,true);
end;
procedure TFrameTree.tvTreeChange(Sender: TObject; Node: TTreeNode);
begin
if pdataset.State in [dsInsert,dsEdit] then pDataset.Post;
if node<>nil then
if pos('_',node.Text)>0 then
if not Pdataset.Locate(PID.FieldName,GetCommaStr(node.text,1,'_'),[]) then
raise exception.Create('记录未找到!');
end;
procedure TFrameTree.TreeDeleteItem(node: TTreeNode);
begin
tvTree.Items.Delete(node);
end;
function TFrameTree.TreeFindItem(ID:string): TTreeNode;
var
node:TTreeNode;
begin
Node:= tvTree.Items.GetFirstNode;
if (Node<> nil) and (GetCommaStr(Node.text,1,'_') <> ID) then
repeat
Node := Node.GetNext;
until (Node = nil) or (GetCommaStr(Node.text,1,'_')= ID);
Result := Node;
end;
end.
调用生成
cdsTree.Open;
FrameTree1.TreeInit(cdsTree,cdsTree.FieldByName(CodeField),cdsTree.FieldByName(NameField),cdsTree.FieldByName(ParentField));
node:=FrameTree1.tvTree.Items.Add(nil,TopText);
node.HasChildren:=true;
node.Expand(false);
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询