简体   繁体   English

如何使用不在列表中的值实现TDataSet绑定的组合框

[英]How to implement a TDataSet-bound combobox with not-in-list values

I need to implement a combobox, which is bound to a TpFIBDataSet (descendant of TDataSet ). 我需要实现一个组合框,该组合框绑定到TpFIBDataSetTDataSet后代)。 I've done this several times before. 我已经做过几次了。 It's not a big thing if it contains only predefined values. 如果它仅包含预定义的值,那不是什么大事。

This time, I'd like to have a combobox that accepts custom values entered by the user, also giving the ability to the user to select some predefined value. 这次,我希望有一个组合框可以接受用户输入的自定义值,同时还使用户能够选择一些预定义的值。 Newly entered values shall be inserted into some table of the database just before the record the combobox's field belongs to is posted. 在张贴组合框字段所属的记录之前,应将新输入的值插入数据库的某个表中。

The main problem seems to me, that predefined values are internally represented as integer IDs (the combobox I use is TwwDBComboBox from Roy Woll's InfoPower package, as it implements maplist functionality) because the field is a foreign key, while custom values may be nearly everything (only restricted by a mask). 主要的问题在我看来,是预先定义的值在内部表示为整数ID(我用的是组合框是TwwDBComboBox罗伊沃尔的InfoPower包,因为它实现了MAPLIST功能),因为该领域是一个外键,而自定义值可能是几乎所有(仅受蒙版限制)。
How can I distinguish between an integer ID and integer user-input, for example? 例如,如何区分整数ID和整数用户输入?

See the set properties of the combobox: 请参阅组合框的设置属性:

AComboBox.Style   := csDropDown;
AComboBox.MapList := True;

I don't request a solution as take this piece of code and be happy . 我不需要解决方案,因为这段代码很高兴 I'm rather looking for some advice by others who might have or had a similar problem. 我是在寻找其他可能有或有类似问题的人的建议。

How can I distinguish between an integer ID and integer user-input, for example? 例如,如何区分整数ID和整数用户输入?

You go back to the database. 您回到数据库。 Either query directly select count(*) from table where id = ComboBoxId . 任一查询都直接select count(*) from table where id = ComboBoxId
Or use the Locate method of the dataset. 或使用数据集的Locate方法。
Or keep a cache handy in a MyList: TList<Integer> and do a MyList.BinarySearch to see if the item is already in the DB. 或在MyList: TList<Integer>保留一个方便的缓存MyList: TList<Integer>并执行MyList.BinarySearch以查看该项目是否已在数据库中。
Obviously the cache will only work if the DB is single-user, because otherwise you will not be able to keep it up-to-date. 显然,仅当数据库为单用户时,缓存才起作用,因为否则您将无法使其保持最新状态。

If it is not in the DB, you run the insert query. 如果它不在数据库中,则运行插入查询。 After it's inserted you run the default combobox behavior, because now the values is sure to be in the DB. 插入后,您将运行默认的组合框行为,因为现在这些值肯定已在数据库中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM