TreeView通过读数据库里的记录创建节点

原创 2006年05月18日 14:04:00

把菜单项都存在一个数据表里,这个数据表可以有无限级,通过读这个数据表来创建 Treeview 的节点
同时包括对节点的增删改


表结构

  create table HRDict( 
  id                int identity(1,1), 
          ModuleName        char(12), 
  ParentId          int 为 -1 则代表是根
  ) 

//-----------------------------------------------------


unit DictUnit;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, ComCtrls, StdCtrls,dbtables;

type
  TNodeId = record
    id : integer;
  end;
  TDictForm = class(TForm)
    Panel1: TPanel;
    TreeView1: TTreeView;
    edContent: TEdit;
    btnNewSave: TButton;
    btnModifySave: TButton;
    btnRemove: TButton;
    btnClose: TButton;
    Button1: TButton;
    procedure FormCreate(Sender: TObject);
    procedure btnNewSaveClick(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure btnModifySaveClick(Sender: TObject);
    procedure btnRemoveClick(Sender: TObject);
    procedure btnCloseClick(Sender: TObject);
  private
    procedure showNodes();             // 显示节点
    procedure CreateChildNode(ParentNode:TTreeNode;parentID:integer);         // 建立子节点

    procedure NewSave(isSameLevel:boolean);
    procedure ModifySave();
    procedure DeleteSave();
  public
    { Public declarations }
  end;

var
  DictForm: TDictForm;

implementation

uses DMUnit;

{$R *.dfm}

procedure TDictForm.showNodes();
var
  node:TTreeNode;
  nodeId:TNodeid;
begin
  self.TreeView1.Items.Clear;
  with DmForm.HRDictQry do
  begin
    if active then close;
    sql.Clear;
    sql.Add('select * from HRDict where parentId=-1');
    open;
    if recordcount=0 then exit;

    while not eof do
    begin
      node := treeView1.Items.Add(nil,FieldByName('moduleName').AsString);
      nodeID.id := FieldbyName('id').AsInteger;
      node.Data := Pointer(nodeid.id);
      self.CreateChildNode(node,FieldByName('id').AsInteger);
      next;
    end;
    close;

  end;
 
end;

procedure TDictForm.CreateChildNode(parentNode:TTreeNode;ParentID:integer);
var
  tempQry : TQuery;
  node:TTreeNode;
  nodeID:TNodeID;
begin
  tempQry := TQuery.Create(nil);
  tempQry.DatabaseName := DMForm.HrmsDataBase.DatabaseName;
  try
    with tempQry do
    begin
      sql.Add('select * from HRDict where parentid='+IntToStr(parentid));
      open;
      if recordcount=0 then exit;
      while not eof do
      begin
        node := self.TreeView1.Items.AddChild(parentnode,
        FieldByName('modulename').AsString);
        nodeId.id := FieldByName('id').AsInteger;
        node.Data := pointer(nodeid.id);
        self.CreateChildNode(node,FieldByName('id').AsInteger);
        next;
      end;
    end;
  finally
    tempQry.Free;
  end;
end;

procedure TDictForm.NewSave(isSameLevel:boolean);
var
 //pid:integer;        // parentId
 id :integer;
begin
  if trim(self.edContent.Text)='' then exit;

  id := integer(treeview1.Selected.Data);
  with DMform.HRDictQry do
  begin
    if isSameLevel then      // 是同一级别
    begin
      if active then close;
      sql.Clear;
      sql.Add('select parentid from HRDict where id='+IntToStr(id));
      open;
      id := Fields[0].AsInteger;
    end;
    close;
    sql.Clear;
    sql.Add('insert HRDict(moduleName,parentid)values'
    +'('''+self.edContent.Text+''','
    +inttostr(id)+')');
    execsql;
    self.showNodes;
  end;
end;

procedure TDictForm.ModifySave;
var
  id :integer;
  ss :string;
begin
  id := integer(self.TreeView1.Selected.Data);
  ss := trim(self.edContent.Text);
  if ss = '' then exit;
  with DMForm.HRDictQry do
  begin
    if active then close;
    sql.Clear;
    sql.Add('update hrdict set moduleName='''+ss+''' where id='+intTostr(id));
    execsql;
    self.showNodes;
  end;
end;

procedure TDictForm.DeleteSave;
var
  id :integer;
begin
  if messagebox(0,pchar('确认要删除吗?'),pchar('提示'),mb_yesno+mb_iconquestion)=mryes then
  begin
    id := integer(self.TreeView1.Selected.Data);
    with DMForm.HRDictQry do
    begin
      if active then close;
      sql.Clear;
      sql.Add('delete hrdict  where id='+intTostr(id)+' or parentid='+inttostr(id));
      execsql;
      self.showNodes;
    end;
  end;
end;

procedure TDictForm.FormCreate(Sender: TObject);
begin
  self.showNodes;
end;

procedure TDictForm.btnNewSaveClick(Sender: TObject);
begin
  self.NewSave(true);
end;

procedure TDictForm.Button1Click(Sender: TObject);
begin
  self.NewSave(false);
end;

procedure TDictForm.btnModifySaveClick(Sender: TObject);
begin
  self.ModifySave;
end;

procedure TDictForm.btnRemoveClick(Sender: TObject);
begin

  self.DeleteSave;
end;

procedure TDictForm.btnCloseClick(Sender: TObject);
begin
  close;
end;

end.

相关文章推荐

C#TreeView连接SQL数据库(可实现无限节点)

转自:http://hi.baidu.com/wenjunlin/item/031ce251effa16a8adc85716 学习编程的秘诀是:编程,编程,再编程! ...

动态从数据库中绑定TreeView节点

  • 2010年04月28日 10:56
  • 90KB
  • 下载

创建TreeView控件,响应NM_CLICK消息得到当前点击的节点的信息(SDK版本)

// 创建TreeView控件 case WM_CREATE: { INITCOMMONCONTROLSEX icc = {sizeof(icc), ICC_TREEVIEW_CLAS...

ASP.NET 读数据库绑定到 TreeView 递归方式

创建表及插入模拟数据: CREATE TABLE [sysMenuTree]( [NoteId] [decimal](18, 0) NOT ...

c# treeview控件动态绑定数据库,实现2级目录,当输入同一父节点的子节点时,在相同父节点下添加子节点.txt

 private void LoadData()        {            try            {                           //得到所有所有父节点,...

树状图 TreeView控件--添加/删除节点(亲自实践)

新增节点         '设定树状图中节点         Dim node As TreeNode         'Method   1:   straightforward   addi...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:TreeView通过读数据库里的记录创建节点
举报原因:
原因补充:

(最多只允许输入30个字)