如何在 Delphi DBLookupComboBox 中选择正确的项目

发布于 2024-12-01 19:31:49 字数 403 浏览 0 评论 0原文

我有一个连接到数据库查询的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)

enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

人海汹涌 2024-12-08 19:31:49

你可以尝试这个(我知道你现在可能已经解决了这个问题,正如你两年多前问的那样),但万一其他人感兴趣......

dbluLookup.KeyValue := dbluLookup.ListSource.DataSet.FieldByName(dbluLookup.KeyField).Value;

这只是将 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...

dbluLookup.KeyValue := dbluLookup.ListSource.DataSet.FieldByName(dbluLookup.KeyField).Value;

That simply sets KeyValue to the first record in the ListSource dataset, which should be the row 'Please Select'.

穿透光 2024-12-08 19:31:49

我的猜测是基础表字段的值是 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...

作妖 2024-12-08 19:31:49

执行此操作的一种方法是将“请选择”添加到您要从中选择的基础表中,其中该元组的键为 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!

握住你手 2024-12-08 19:31:49

DBLookupComboBox 默认显示 ListField(s),其 ListSource 中的 KeyFieldDataField< /code> 在 DataSource 中。因此,为了确保显示 NULL 的 DataField 值,您必须在 ListSource 中提供 NULL 的 KeyField。但是可以想象,与 ListSource 关联的底层表中不希望出现 NULL 值。

避免这种情况的一种方法是使用 UNION SELECT 将 NULL 值添加到 ListSource 后面的数据集。这应该可以正常工作,因为该数据集不必是可编辑的。

现在,为了确保此特殊数据集仅在您向与 DataSource 关联的数据集添加新记录时可用,请在 中管理 ListSource.DataSet 的查询DataSource.OnStateChange。当DataSource.State = dsInsert时,则更新ListSource.DataSet

The DBLookupComboBox displays by default the ListField(s) whose KeyField in the ListSource matches the DataField in the DataSource. So, to ensure to display some value for the DataField being NULL, you have to provide a KeyField of NULL in the ListSource. But one could imagine not wanting a NULL value in the underlaying table associated to the ListSource.

One way to circumvent this is to add the NULL value to the dataset behind the ListSource with a UNION 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 for ListSource.DataSet in DataSource.OnStateChange. When DataSource.State = dsInsert, then update ListSource.DataSet.

仙女山的月亮 2024-12-08 19:31:49

修改 TwwDBLookupCombo、InfoPower 组件中 Steve Childs 的答案

cboFilterTravel1.LookupValue := cboFilterTravel1.LookupTable.FieldByName(cboFilterTravel1.LookupField).Value;

谢谢 Steve 这对我有用

Modification for Steve Childs answer in TwwDBLookupCombo, InfoPower component

cboFilterTravel1.LookupValue := cboFilterTravel1.LookupTable.FieldByName(cboFilterTravel1.LookupField).Value;

Thanks Steve It's works for me

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文