delphi TcxDBTreeList做一个带树状结构的下拉框(基于数据库)

注:本篇内容与上一篇内容有部分关联:

https://www.cnblogs.com/yoooos/p/16610379.html

效果要求:

一.每次点击cxPopupEdit1时,弹出树状结构

二.树状结构的数据为数据库实时数据

三.树状结构显示时自动展开

四.双击树状结构,把返回值赋值给cxPopupEdit1控件

成品效果图:

双击包装部,返回值赋给cxPopupEdit1

 

 

 

设计图:

cxPopupEdit1关联树状结构:

properties---popucontrol---选择cxDBTreeList2控件

cxPopupEdit1点击事件:

properties---onInitPopup事件

procedure TForm1.cxPopupEdit1PropertiesInitPopup(Sender: TObject);
begin
  FDQuery1.Close;
  FDQuery1.Open;  //保证数据为实时数据
  cxDBTreeList2.FullExpand;  //自动展开
end;

cxDBTreeList2的visible属性设置为false,然后设置双击事件:

procedure TForm1.cxDBTreeList2DblClick(Sender: TObject);
begin
  //不要再在树状结构上乱搞啦,我说过很多次啦.应该直接访问数据集
 If FDQuery1.Recordcount>0 then   cxPopupEdit1.Text := FDQuery1.FieldByName('组织名称').AsString;     cxPopupEdit1.DroppedDown := False;  //收起菜单 end;

完成!

 2025-10-22 补充

一.关于"图标ID" 字段. 有网友问我为什么他的图标显示不出来,我在这里把细节补充一下:

TcxDBTreeList-images属性需要关联一个cximageList控件,并且cximageList控件选择图标时,不要选字体图标,虽然不会报错,但是会显示异常

image

image

 

二.关于动态创建TcxDBTreeList项目,核心代码如下

procedure TCK.GetData();
begin
  with FDQuery1 do
  begin
    close;
    sql.Text := 'select * from 仓库列表 where 启用状态=-1 order by 权重 desc';
    Open();
  end;
  with cxDBTreeList1 do
  begin
    clear;
    Bands.Clear;
    DataController.ImageIndexField := '图片ID';
    DataController.KeyField := '仓库ID';
    DataController.ParentField := '父级ID';
    DataController.StateIndexField := '仓库名称';
    images := FData.cxImageList1;

    var band := Bands.add;
    var col := CreateColumn(band);
    TCXDBTREELISTcolumn(col).DataBinding.FieldName := '仓库名称';
    TCXDBTREELISTcolumn(col).DataBinding.ValueType := 'string';
    TCXDBTREELISTcolumn(col).Width := parent.Width;
  end;

end;

注意TCXDBTREELISTcolumn(col) 这种写法.如果直接写col.DataBinding.FieldName 会提示 没有FieldName 属性.

 

posted @ 2022-08-24 10:17  一曲轻扬  阅读(549)  评论(0)    收藏  举报