Delphi TreeView 使用方法。连接access数据库

TreeView连接了一个access数据库,并且可以通过两个DBEdit1,对TreeView进行操作,同时也对access数据库的数据进行操作。这里有图(http:/... TreeView 连接了一个access数据库,并且可以通过两个DBEdit1,对TreeView 进行操作,同时也对access数据库的数据进行操作。
这里有图 (http://wenwen.soso.com/z/q210884984.htm
请帮忙,谢谢。
把源码发下,也把程序发给下 443030595@qq.com
展开
 我来答
放飞另一个心灵
2010-08-06
知道答主
回答量:19
采纳率:0%
帮助的人:19.1万
展开全部
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ComCtrls, DB, ADODB, Mask, DBCtrls;

type
TForm1 = class(TForm)
TreeView1: TTreeView;
Button1: TButton;
Button2: TButton;
Button3: TButton;
ADOConnection1: TADOConnection;
ADOQuery1: TADOQuery;
DataSource1: TDataSource;
DBEdit1: TDBEdit;
DBEdit2: TDBEdit;
TempQy: TADOQuery;
TempDs: TDataSource;
Button4: TButton;
procedure InitList;
procedure FormShow(Sender: TObject);
procedure TreeView1Change(Sender: TObject; Node: TTreeNode);
procedure Button1Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
Function FindMaxCode(CodLength:Integer;ParentCode:string):string;
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

type myint = ^string;

var
pt : myint;

implementation

{$R *.dfm}

//查找最大的代码
Function TForm1.FindMaxCode(CodLength:Integer;ParentCode:string):string;
var
SqlStr:string;
begin
result:='';
SqlStr:='select max(DepartmentCode) as MaxCode from CodeDepartment';
SqlStr:=SqlStr+' where len(DepartmentCode)='+#39+inttostr(CodLength)+#39;
SqlStr:=SqlStr+' and ParentCode='+#39+ParentCode+#39;
try
TempQy.Close;
TempQy.SQL.Clear;
TempQy.SQL.Text:=SqlStr;
TempQy.Open;
except
APPLICATION.MessageBox ('查找最大的代码失败!','提示',16);
exit;
end;
result:=TempQy.FieldByName('MaxCode').AsString;
end;

//初始化列表
procedure TForm1.InitList;
var node : ttreenode;
sbm,sbm1,smc : string;
SqlStr : string;
s : integer;
begin
node := nil;
sbm := '21';
//--读取列表
SqlStr:='select DepartmentCode,DepartmentName from CodeDepartment';
SqlStr:=SqlStr+' order by DepartmentCode';
try
TempQy.Close;
TempQy.SQL.Clear;
TempQy.SQL.Text:=SqlStr;
TempQy.Open;
except
APPLICATION.MessageBox ('与服务器连接失败,无法进入业务处理!','提示',16);
exit;
end;
TempQy.First;
TreeView1.Items.Clear;
while not TempQy.Eof do //循环数据表
begin
sbm1 := TempQy.FieldByName('DepartmentCode').asstring;
smc := TempQy.FieldByName('DepartmentName').asstring;
new(pt);
pt^ := TempQy.fieldbyname('DepartmentCode').asstring;
if length(sbm1) = 2 then //添加根结点
node := TreeView1.Items.AddObject(nil,smc,pt)
else if length(sbm1) = length(sbm) then //同级结点
node := TreeView1.Items.AddObject(node,smc,pt)
else if length(sbm1) > length(sbm) then //下级结点
node := TreeView1.Items.AddChildObject(node,smc,pt)
else if length(sbm1) < length(sbm) then //查找上一个结点
begin
s := length(sbm);
while length(sbm1) <> s do
begin
s := s - 2;
node := node.Parent;
end;
node := TreeView1.Items.AddObject(node,smc,pt);
end;
node.ImageIndex := 1;
sbm := sbm1;
TempQy.Next;
end;
if TreeView1.Items.GetFirstNode <> nil then
TreeView1.Items.GetFirstNode.Selected := true;
end;

procedure TForm1.FormShow(Sender: TObject);
begin
ADOQuery1.close;
ADOQuery1.Open;
InitList;
end;

//选中定位
procedure TForm1.TreeView1Change(Sender: TObject; Node: TTreeNode);
var
s1 : string;
begin
s1 := myint(TreeView1.Selected.Data)^;
ADOQuery1.Locate('DepartmentCode',s1,[]);
end;

//增加根结点
procedure TForm1.Button1Click(Sender: TObject);
var
CodLength:Integer;
ParentCode,DepartmentCode:string;
begin
CodLength:=2;
ParentCode:='0';
DepartmentCode:=inttostr(strtoint(FindMaxCode(CodLength,ParentCode))+1);
if length(DepartmentCode)=1 then
DepartmentCode:='0'+ DepartmentCode;
ADOQuery1.Append;
ADOQuery1.Edit;
ADOQuery1.FieldByName('DepartmentCode').AsString :=DepartmentCode;
ADOQuery1.FieldByName('ParentCode').AsString :=ParentCode;
end;

//保存
procedure TForm1.Button4Click(Sender: TObject);
begin
ADOQuery1.Post;
InitList;
end;

//增加结点
procedure TForm1.Button2Click(Sender: TObject);
var
CodLength:Integer;
ParentCode,DepartmentCode:string;
begin
if length(ADOQuery1.FieldByName('DepartmentCode').AsString)=6 then
begin
showmessage('本系统只支持三级目录,不能在三级目录下增加结点!');
exit;
end;
CodLength:=length(ADOQuery1.FieldByName('DepartmentCode').AsString);
ParentCode:=ADOQuery1.FieldByName('DepartmentCode').AsString;
ADOQuery1.Append;
ADOQuery1.Edit;
if FindMaxCode(CodLength+2,ParentCode)='' then
DepartmentCode:=ParentCode+'01'
else
DepartmentCode:=inttostr(strtoint(FindMaxCode(CodLength+2,ParentCode))+1);
ADOQuery1.FieldByName('DepartmentCode').AsString :=DepartmentCode;
ADOQuery1.FieldByName('ParentCode').AsString :=ParentCode;
end;

end.

DepartmentCode DepartmentName ParentCode
01 单位一 0
0101 部门一 01
010101 科室一 0101
010102 科室二 0101
0102 部门二 01
warmwormn
2010-08-05 · TA获得超过737个赞
知道小有建树答主
回答量:1694
采纳率:0%
帮助的人:1341万
展开全部
说啥好
离开了DB控件
连数据库都不会操作了

数据库里取数据 放到TREEVIEW里
TREEVIEW里的操作映射到数据库操作
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式