Board index » delphi » Making a Combobox selection based on data coming from a database

Making a Combobox selection based on data coming from a database

Hello,

I am trying to get one of the delphi combo boxes to get populated with data
from a database. I want the user to be able to look at data coming from the
database when he clicks the combo box to choose among items of that data. I
know Deplhi comes witgh TComboBox which is not data aware, TDBCombobox which
is data aware on the edit box but non-data aware on the dropdown and
TDBLookupCombobox which is data aware in both. So far the only way I can
think of to do what I want is to use TComboBox and have it populated with
data from a database using a routine. Can someone tell me if there is a
better way?

Thanks!

Dimitri

 

Re:Making a Combobox selection based on data coming from a database


Quote
In article <39d48ab7_1@dnews>, Dimitris Kourkoulis wrote:
> So far the only way I can
> think of to do what I want is to use TComboBox and have it populated with
> data from a database using a routine.

That is the way to do it and it is really not *that* complicated. You can
write a simple tool procedure to do the task:

 Procedure LoadComboFromDataset( cb: TCombobox; ds: TDataset; fieldindex:
Integer );
 Begin
   Assert( Assigned( cb ));
   Assert( Assigned( ds ));
   Assert( ds.Active );
   Assert( (fieldindex >= 0 ) and (fieldindex < ds.fieldcount ));

   ds.First;
   while not ds.eof do begin
     cb.items.add( ds.Fields[fieldindex ].AsString );
     ds.next;
   end;
 End;

 LoadComboFromDataset( combobox1, query1, 0 );

Untested!

Peter Below (TeamB)  100113.1...@compuserve.com)
No e-mail responses, please, unless explicitly requested!

Re:Making a Combobox selection based on data coming from a database


Is there an easy way to make the drop list multi-column, and then enable
your procedure to receive an array of field indexes in order to populate the
appropriate columns? Of course this should probably become a new component
(?).

-- Petros Amiridis

Re:Making a Combobox selection based on data coming from a database


Quote
In article <39d82ee9_1@dnews>, Petros Amiridis wrote:
> Is there an easy way to make the drop list multi-column, and then enable
> your procedure to receive an array of field indexes in order to populate the
> appropriate columns? Of course this should probably become a new component

One could fake something using an owner-drawn combobox but this is a control
that is notoriously difficult to modify beyond a certain point (adding a header
to the dropdown list would be beyond that point <g>) so it is propably easier
to build a new control from scratch. Look at TDBLookupCombobox for an example.

Peter Below (TeamB)  100113.1...@compuserve.com)
No e-mail responses, please, unless explicitly requested!

Other Threads