代码之家  ›  专栏  ›  技术社区  ›  maf-soft

如何允许在TDBLookupComboBox中选择空值?

  •  2
  • maf-soft  · 技术社区  · 7 年前

    我有一个 TDBLookupComboBox 显示 TStringField fkLookup Null 值(来自可为null的整数数据库列)。

    下拉列表显示分配的项目 LookupDataSet ,它来自联接表。如果该字段是 无效的 ,则不显示任何列表项,组合框为空。如果字段有值,则显示正确的描述。

    无效的 按指定的 NullValueKey

    没关系,但是用户更喜欢使用鼠标。所以我可以提供一个清晰的按钮,但我认为在列表顶部增加一行会更好。我该怎么做?

    3 回复  |  直到 7 年前
        1
  •  3
  •   halfer Jatin Pandey    7 年前

    您可以将空行放入查询中,如果需要对其进行排序,可以使其显示在列表的顶部,如下所示:

    select 0 as sort,
           convert(int, null) as UserID,
           'Clear' as Name
    union all
    select 1 as sort,
           u.UserID,
           u.Name
    from   tblUser u
    order by sort, Name
    

    sort 列将使其显示在顶部,然后您可以根据需要进行排序。

        2
  •  1
  •   Miamy Dan Barrett    7 年前

    你可以用你的 LookupDataSet

    SELECT CAST (NULL AS INTEGER) AS ID, CAST ('clear' AS VARCHAR(80)) AS NAME FROM table_name
    UNION
    SELECT ID, NAME FROM table_name
    

    然而,在这个实现中 clear 项目将位于列表的末尾。

    恐怕我们永远不会达到有空值字段的行为,如 正常的 https://petercai.com/null-is-not-a-value/ . 我们只能做些变通办法。

    SELECT CASE WHEN OurField IS NULL THEN '(empty)' ELSE OurField END AS OurField . 我们可以用人工菜单项设置为空。但这并不是一个完整、复杂的解决方案。

        3
  •  1
  •   maf-soft    7 年前

    这个 为开发人员提供更好的解决方案 第三方控件 TcxDBLookupComboBox 它可以提供一个很好的组合框内清晰的按钮!

    Screenshot of a TcxDBLookupComboBox/IsFixedList with clear button

    它的工作原理和 TcxButtonEdit Buttons 属性未在中公开 ,因此只能在运行时设置:(

    procedure TForm1.FormCreate(Sender: TObject);
    begin
      AddClearButton(cxDBLookupComboBox1.Properties);
    end;
    
    procedure TForm1.AddClearButton(Prop: TcxCustomEditProperties);
    begin
      with Prop.Buttons.Add do begin
        Kind:= bkText;
        Caption:= #$2715; //Unicode X-symbol
      end;
      Prop.OnButtonClick:= ClearButtonClick;
    end;
    
    procedure TForm1.ClearButtonClick(Sender: TObject; AButtonIndex: Integer);
    begin
      if AButtonIndex = 1 then
        with TcxCustomEdit(Sender) do begin
          EditValue:= Null;
          PostEditValue;
        end;
    end;
    

    这可能也适用于其他编辑控件,但至少在TcxDBSpinEdit中不适用。

    推荐文章