How do I get the Proper Item in a Delphi DBLookupComboBox to be Selected

14,028

Solution 1

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...

Solution 2

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'.

Solution 3

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!

Solution 4

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.

Share:
14,028
Michael Riley - AKA Gunny
Author by

Michael Riley - AKA Gunny

Helping people get out of debt, one debt at a time. http://zilchworks.com Software Products: Zilch Standard - Debt Reduction Software Debt Manager Profesional Credit Card Math Favorite Languages: 1. Delphi / Object Pascal 2. SQL 3. VBScript / ASP

Updated on June 16, 2022

Comments

  • Michael Riley - AKA Gunny
    Michael Riley - AKA Gunny almost 2 years

    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.