控件treeview的使用

类别:Delphi 点击:0 评论:0 推荐:

procedure Tcpya_app.FormCreate(Sender: TObject);
begin
  datamodule2.tree.close;
  datamodule2.tree.sql.clear;
  datamodule2.tree.sql.add('select name from plat order by name');
  datamodule2.tree.Active:=true;
  datamodule2.tree.Open;
  while not datamodule2.tree.Eof do
    begin
     with tv.Items do
  begin
     MyTreeNode1 := Add(nil,trim(datamodule2.tree.fields[0].asstring));//增加根节点
      with datamodule2 do
      begin
       child.close;
       child.SQL.Clear;
       child.SQL.Add('select comp from dalei where plat='''+Trim(datamodule2.tree.Fields[0].asstring)+''''+' order by comp');
       child.Active:=true;
       child.Open;
       While Not child.Eof do
       begin
            TV.Items.AddChild(mytreenode1,trim(child.Fields[0].AsString));
            child.Next;
            end; end;
       datamodule2.tree.Next;
   end;
   end;
end;

在添加和删除前必须保证有节点被选中(Treeview.Selected= nil)

用Add,如:MyTreeNode1 := Add(nil,trim(datamodule2.tree.fields[0].asstring));

在添加子节点时用addchild(父节点,子节点名),

但在添加大量的数据的时候 最好使用

TreeView.Items.BeginUpdate;

添加节点

TreeView.Items.EndUpdate

 

删除节点

Treeview.Selected.Delete

编辑节点内容

Treeview.Selected.EditText

本文地址:http://com.8s8s.com/it/it5307.htm