Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 391
  • Last Modified:

Records in TDBComboBox

I want see all records of one field using TDBComboBox.
How can I do that?

Thanks....
0
sergio_2001
Asked:
sergio_2001
  • 4
  • 4
  • 2
1 Solution
 
bijoynCommented:
Hi There,

If I have understood your question right then I am giving u a working e.g. In the e.g I have a TDBcomboBox which is linked to a particular Datasource. There is another datasource fropped to access all the records from the desired table. I have defined one query to access the desired field from the desired table. On the dropdown event of the the Combobox I am adding each value to the ComboBox. U can do even on formshow or some other suitable place. The only thing u will have to check is that It should execute the code only once in the form. Right now it will execute everytime u dropdown. By setting a flag u can easily achieve it. I hope u find the solution useful.

---------------------------------------

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ComCtrls, StdCtrls, DBCtrls, DB, DBTables;

type
  TForm1 = class(TForm)
    DataSource1: TDataSource;
    Query1: TQuery;
    DBComboBox1: TDBComboBox;
    DataSource2: TDataSource;
    Query2: TQuery;
    procedure DBComboBox1DropDown(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.DBComboBox1DropDown(Sender: TObject);
begin
   Query1.First;
   while not(Query1.Eof) do
   begin
      DbComboBox1.Items.Add(Query1.Fields[0].AsString);
      Query1.Next;
   end;
end;

end.

-------------------------------------------------
0
 
ZifNabCommented:
Hi sergio_2001,
Yo Bijoyn!

If you don't have enough information with Bijoyn's example, then you can read this :

Filling dblistboxs and dbcomboboxs

Most of Delphi's data aware components will  populate
themselves after they are wired up to a open dataset.  However
DbListboxs and DbComboboxs do not display this characteristic.  
These two components are not for displaying your datasets, but
filling them.  Use of these components is straight forward.  
When you update your table, the value of the DbListbox or
DbCombobox will be posted in the appropriate field.

Filling the DbCombobox or DbListbox the same as filling normal
comboboxs or listboxes.  The lines of text in a listbox or
combobox are really a tstring list.  The "Items" property of
the given component holds this list.  Use the "Add" method for
adding items to a tstring. If you want to use data  types other
than strings they must be converted at run time. If your list
has a blank line at the end, consider setting the
"IntegralHeight" property to True.


Filling a DbListbox with 4 lines programmatically might look
similar to this:

     DbListbox1.items.add('line one');
     DbListbox1.items.add('line two');
     DbListbox1.items.add('line three');
     DbListbox1.items.add('line four');

Filling a DbListbox at design time requires using the object
inspector.  By double clicking on the components "Items"
property, you can bring up the "String List Editor" and input
the desired rows.

Unfortunately, if a combobox is filled this way, there is not
default value.  Setting a DbComboboxs "text" property will
achieve this result.  (the "text" property is not available in
the object inspector, so it must be set programmatically).  
Setting the default value to the first value in the
DbCombobox's list looks like this:        

DbCombobox1.text := DbCombobox1.items[0];

Often it is useful to fill a DBListBox from a dataset.  This
can be done using loop:

procedure TForm1.FormCreate(Sender: TObject);
begin
  with table2 do begin
    open;
    while not EOF do
    begin
      DBlistbox1.items.add(FieldByName('name').AsString);
      next;
    end;
  end;
end;

Have a nice day,
Regards Zif.
0
 
sergio_2001Author Commented:
Hi all,

I'll evaluate both answers.

Thanks.....
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
sergio_2001Author Commented:
Hi all,

All the answers solve. But there are two problems:
- When I open the dbcombobox there are um blank space in the first item(0).
- I can't select one other item than the current item.

Points increased.

Thanks....

0
 
ZifNabCommented:
The state of the underlying datafield  is not readonly?
0
 
bijoynCommented:
Hi Sergio, Yo Zifnab,

If the first record in the combobox is showing blanks then please check if the records in the source table is correct. As for the selection the dataset to which your ComboBox points to, must be edtiable ( ReadOnly = False as Zif as put in the above comment ).

Bijoy
0
 
sergio_2001Author Commented:
Hi bijoyn , Zifnab,

- The dataset of records looks good...
- I have the readonly property set to false...

but doesn't works...

Please send your comments...

Thanks...
0
 
bijoynCommented:
Hi Sergio,

I think I know what the problem is. Firstly go to the object inspector for your DBComboBox, Click on the items property, U might find a blank line in it. If its there then thats what results in the first blank record in the comboBox. Secondly check the dataset associated with your DbCombobox. It might have its RequestLive property set to false, make it true. If u do both of these things then ur problem will be solved.

Cheers,

Bijoy
0
 
sergio_2001Author Commented:
Hi all,

Bijoy....

- You are right... I deleted the first item from items property in the object inspector and it works fine....

- About the dbcombobox.... I'm using um ttable dataset... so I don't have the RequestLive property.... I'm using ReadOnly := false... but I can't select and change the record yet...

- But it's good at this moment... send-me the answer....

Thanks.....
0
 
bijoynCommented:
The question is anwsered.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 4
  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now