最近公司项目中用到dxTreelist控件,以前没怎么用过这个控件,该控件比TreeView功能更强大,要完全掌握还真不容易,下面是我个人做的一个Demo,对该控件进行增删改遍历操作,以便自己以后忘了使用方法随时可以查吧。
1.首先新建一个数据库。
打开SQL Server2005,新建数据库TreeTest,新建表DeviceTab,表的字段设计如下:
2.新建Delphi应用程序
选择File-New-Application新建应用程序,File-New-Data Module,新建一个数据库模块,添加TADOConnection和TImageList控件,设置ADOConnection的LoginPrompt为false,给ImageList控件添加7个图片,该数据模块的Create事件代码如下:
procedure Tdm.DataModuleCreate(Sender: TObject);
begin
if not con1.Connected then
begin
//设置连接字符串
con1.ConnectionString:='Provider=SQLOLEDB.1;Password=123456;Persist Security Info=True;User ID=sa;Initial Catalog=TreeList;Data Source=ZH\SQLEXPRESS;';
con1.Open; //打开数据库连接
end;
end;
在窗体上放置dxTreeList控件和四个Button控件以及一个Timer控件设置Timer控件的Interval属性为100,各按钮相关事件及源码如下:
unit Main;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, dxExEdtr, dxCntner, dxTL, dxTLClms,ADODB, Menus,
cxLookAndFeelPainters, StdCtrls, cxButtons, ExtCtrls;
type
TfrmTree = class(TForm)
dxTreeList1: TdxTreeList;
btnAddNode: TcxButton;
btnDelNode: TcxButton;
btnUpdate: TcxButton;
btnbl: TcxButton;
tmr1: TTimer;
procedure btnblClick(Sender: TObject);
procedure btnAddNodeClick(Sender: TObject);
procedure btnDelNodeClick(Sender: TObject);
procedure btnUpdateClick(Sender: TObject);
procedure tmr1Timer(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
procedure CreateColumn(tree:TdxTreeList;list:TStrings);
procedure CreateImageColumn(tree:TdxTreeList;Image:TImageList);
procedure FillTree(node:TdxTreeListNode;pid:Integer;con:TADOConnection);
function GetMaxId(con:TADOConnection):Integer;
procedure DelTree(pid:Integer;con:TADOConnection);
procedure UpdateTree(newname:string;id:Integer;con:TADOConnection);
function GetCount(tree:TdxTreeList):Integer;
end;
var
frmTree: TfrmTree;
implementation