DELPHI cxgrid中,创建下拉菜单与自动计算字段

一.下拉菜单.

需求:字段为Integer类型,但要求显示出文本内容.如下图,仓库实际存储的是仓库ID,显示的却是仓库名称.

6e7217ab22ab32f266405ce99c59a2a624cecbadafb69b73246d13ab6dfb5aa2

 

 实现方法:

procedure TfraModuleCPRK.SetupWarehouseLookup;
begin
  if not Assigned(CK) or not Assigned(CK.Properties) then
    Exit;

  if not FDQCK.Active then
    FDQCK.Open;

  try
    var LookupProps := TcxLookupComboBoxProperties(CK.Properties);
    LookupProps.KeyFieldNames := '仓库ID';
    LookupProps.ListFieldNames := '仓库名称';

    // 配置列表显示列
    LookupProps.ListColumns.Clear;
    with LookupProps.ListColumns.Add do
    begin
      FieldName := '仓库ID';
      Caption := '仓库ID';
      Width := 50;
    end;

    with LookupProps.ListColumns.Add do
    begin
      FieldName := '仓库名称';
      Caption := '仓库名称';
      Width := 150;
    end;

    // 设置其他属性
    LookupProps.ListFieldIndex := 1;   //显示为仓库名称
    LookupProps.DropDownRows := 10;
    LookupProps.DropDownWidth := 150;
    LookupProps.DropDownAutoWidth := True;
    LookupProps.ImmediateDropDown := True;
    LookupProps.ImmediatePost := True;
    LookupProps.ListOptions.GridLines := glBoth;  //显示所有网络线
    LookupProps.ListOptions.ShowHeader := True;
  except
    on E: Exception do
      ShowMessage('设置仓库列失败:' + E.Message);
  end;
end;

说明,

因为我的数据库是验证登陆的,所以只能在代码下完成这些工作.

如果你的数据库是直联的,可以在设计视图下直接这样设置就可以了,不需要代码

image

 

二.自动计算字段

你是不是这样设置表达式的?

8f11c7c3e0ee20ad41ae02221e0ead14

结果发现在数量和单价发生变动时,金额却没变化?再看个对比图:

1a390b4ce6bcd6bb07683cab8a192497

 原因: 因为计算字段不需要关联 FieldName 属性.

image

 

posted @ 2026-04-12 11:06  一曲轻扬  阅读(15)  评论(0)    收藏  举报