How to get the correct item in Delphi DBLookupComboBox to be selected?

I have a DBLookupComboBox that is connected to a database query. This part is working fine. When I run the program, DBLookupComboBox is populated with query results. I would like to see a DBLookupComboBox filled with the first item . "Select" when the program starts or when the action of a new item is initiated . (See below image)

enter image description here

Also, if I load a previously saved database record that selected Index 2 "Quick Elimination" , how can I get DBLookupComboBox to display this selected record?

Yes, "Select" is index 0 and it will be restored as part of the request.

+4
source share
5 answers

My assumption is that the value of the base field of the table is NULL, and not zero, which indicates to DBComboBox that the value has not yet been selected and it is displayed accordingly.

If the value in the table was zero, I think the text in the combo edit box will be selected to indicate this, but I may not remember it correctly.

In any case, just check Field1.IsNull (or IsEmpty) and then set it to zero. This means that you can no longer distinguish between "unknown value" (NULL) and "no selected value" (zero), unless you prevent the return of a null value to the table ...

+1
source

You could try this (I know that you probably solved it now, as you requested more than 2 years ago), but in case someone else was interested ...

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

This simply sets KeyValue to the first record in the ListSource dataset, which should be the string "Please select".

+3
source

One way to do this would be to add โ€œPlease selectโ€ to the base table from which you select where the key to this tuple will be 0. Then, when you show the drop down field and the value of the associated field is 0, 'Please select' will be displayed. Of course, you must make sure that this value is never selected!

+1
source

DBLookupComboBox displays by default ListField(s) , whose KeyField in ListSource corresponds to ListSource in DataSource . Thus, to display some value for a DataField that is NULL, you must specify KeyField NULL in the ListSource . But you can imagine that you do not need to have a NULL value in the underlay table associated with the ListSource .

One way around this is to add a NULL value to the dataset behind the ListSource using UNION SELECT . This should work fine, as this dataset should not be editable.

Now, to make sure that this special data set is only available when adding a new record to the data set associated with the DataSource , run the query for ListSource.DataSet in DataSource.OnStateChange . When DataSource.State = dsInsert , update ListSource.DataSet .

+1
source

Modification for Steve Baby's answer in TwwDBLookupCombo, InfoPower component

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

Thanks Steve. It works for me.

0
source

Source: https://habr.com/ru/post/1369257/


All Articles