如何在 Delphi DBLookupComboBox 中选择正确的项目
我有一个连接到数据库查询的DBLookupComboBox。那部分工作正常。当我运行该程序时,DBLookupComboBox 会填充查询结果。当程序首次运行时或新项目操作已启动。 (参见下图)
另外,如果我正在加载以前保存的数据库记录,该记录已选择 索引2“快速消除”我如何让DBLookupComboBox显示所选条目?
是的,“请选择” 是索引 0,它作为查询的一部分进行检索。
I have a DBLookupComboBox that is wired to a database query. That part is working fine. When I run the program the DBLookupComboBox is populated with the results of the query. I'd like to to see the DBLookupComboBox populated with the first item "Please Select" when the program first runs or when a new item action is initiated. (See below image)
Also, if I'm loading a previously saved database record that had selected Index 2 "Quick Elimination" how would I get the DBLookupComboBox to display that selected entry?
Yes, "Please Select" is index 0 and it is retreived as part of the query.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
你可以尝试这个(我知道你现在可能已经解决了这个问题,正如你两年多前问的那样),但万一其他人感兴趣......
这只是将 KeyValue 设置为 ListSource 数据集中的第一条记录,这应该是“请选择”行。
You could try this (I know you've probably solved it by now, as you asked over 2 years ago), but in case anyone else was interested...
That simply sets KeyValue to the first record in the ListSource dataset, which should be the row 'Please Select'.
我的猜测是基础表字段的值是 NULL 而不是零,这告诉 DBComboBox 尚未选择任何值并相应地显示。
如果表中的值为零,我认为将选择组合编辑字段中的文本来指示这一点,但我可能错误地记得这一点。
无论如何,只需检查 Field1.IsNull(或 IsEmpty),然后将其设置为零。这确实意味着您无法再区分“未知值”(NULL)和“无选定值”(零),除非您阻止零值返回到表中......
My guess would be that the value of the underlying table field is NULL rather than zero, which tells the DBComboBox that no value has yet been selected and it displays accordingly.
If the value in the table were zero, I thinkg the text in the edit field of the combo would be selected to indicate that, but I may recall this incorrectly.
Anyway, just check for Field1.IsNull (or IsEmpty) and then set it to zero. It does mean that you can no longer distinguish between an "unknown value" (NULL) and "no selected value" (zero), unless you prevent the zero value from making it back into the table...
执行此操作的一种方法是将“请选择”添加到您要从中选择的基础表中,其中该元组的键为 0。然后,当您显示组合框且连接字段的值为 0 时,“将显示“请选择”。当然,您必须确保永远不会选择该值!
One way of doing this would be to add 'Please select' to the underlying table from which you are selecting, where the key to this tuple would be 0. Then when you display the combobox and the value of the connected field is 0, 'Please select' would be displayed. Of course, you have to make sure that this value is never selected!
DBLookupComboBox
默认显示ListField(s)
,其ListSource
中的KeyField
与DataField< /code> 在
DataSource
中。因此,为了确保显示 NULL 的DataField
值,您必须在ListSource
中提供 NULL 的KeyField
。但是可以想象,与ListSource
关联的底层表中不希望出现 NULL 值。避免这种情况的一种方法是使用
UNION SELECT
将 NULL 值添加到ListSource
后面的数据集。这应该可以正常工作,因为该数据集不必是可编辑的。现在,为了确保此特殊数据集仅在您向与
DataSource
关联的数据集添加新记录时可用,请在中管理
。当ListSource.DataSet
的查询DataSource.OnStateChangeDataSource.State = dsInsert
时,则更新ListSource.DataSet
。The
DBLookupComboBox
displays by default theListField(s)
whoseKeyField
in theListSource
matches theDataField
in theDataSource
. So, to ensure to display some value for theDataField
being NULL, you have to provide aKeyField
of NULL in theListSource
. But one could imagine not wanting a NULL value in the underlaying table associated to theListSource
.One way to circumvent this is to add the NULL value to the dataset behind the
ListSource
with aUNION SELECT
. That should work just fine, since that dataset does not have to be editable.Now, to assure this special dataset is only available when you are adding a new record to the dataset associated to the
DataSource
, manage the query forListSource.DataSet
inDataSource.OnStateChange
. WhenDataSource.State = dsInsert
, then updateListSource.DataSet
.修改 TwwDBLookupCombo、InfoPower 组件中 Steve Childs 的答案
谢谢 Steve 这对我有用
Modification for Steve Childs answer in TwwDBLookupCombo, InfoPower component
Thanks Steve It's works for me