1.在Form上添加ComboBox1,ADOQuery1,ADOConnection1(可不要) 2.連接好數(shù)據(jù)庫(kù),南山古桃(nsgtao) 的數(shù)據(jù)庫(kù)中有中有表biao1,有字段name 3.運(yùn)行下面的程序,ComboBox1中會(huì)出現(xiàn)name下對(duì)應(yīng)的東東 procedure TForm1.ComboBox1DropDown(Sender: TObject); var i : integer; begin ComboBox1.Clear; ADOQuery1.Close; ADOQuery1.SQL.Clear; ADOQuery1.SQL.Add('Select name from biao1'); ADOQuery1.Open; if ADOQuery1.RecordCount>0 then begin for i:=0 to ADOQuery1.RecordCount-1 do begin ComboBox1.Items.Add(ADOQuery1.FieldByName('name').AsString); ADOQuery1.Next; end; end; end; 2008年1月4日更新下面內(nèi)容 添加數(shù)據(jù)庫(kù)中字段內(nèi)容到ComboBox procedure TForm1.FormCreate(Sender: TObject); var i : integer; begin ADOQuery1.Close; ADOQuery1.SQL.Clear; ADOQuery1.SQL.Add('select * from biao1'); ADOQuery1.Open; Combobox3.Clear; for i:=0 to ADOQuery1.FieldCount-1 do begin ADOQuery1.First; combobox3.Items.Add(ADOQuery1.Fields[i].FieldName); ADOQuery1.Next; end; Combobox3.ItemIndex := 0; end; 2008.01.15再添加,原文粘貼 //主窗體上放置ADOConnection1,ADOQuery1,DataSource1并有數(shù)據(jù)表,把組件屬性設(shè)置完畢->OK! unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, DB, ADODB, Grids, DBGrids; type TForm1 = class(TForm) ComboBox1: TComboBox; ADOConnection1: TADOConnection; ADOQuery1: TADOQuery; ComboBox2: TComboBox; DBGrid1: TDBGrid; DataSource1: TDataSource; procedure FormCreate(Sender: TObject); procedure ComboBox1Change(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} procedure TForm1.FormCreate(Sender: TObject); var i : integer; begin ADOQuery1.Close; ADOQuery1.SQL.Clear; ADOQuery1.SQL.Add('select * from biao1'); ADOQuery1.Open; //Combobox1***** Combobox1.Clear; if ADOQuery1.RecordCount>0 then begin ADOQuery1.First; end; for i:=0 to ADOQuery1.RecordCount-1 do begin //ADOQuery1.First; ComboBox1.Items.Add(ADOQuery1.Fields[i].FieldName); ADOQuery1.Next; end; Combobox1.ItemIndex := 0; //Combobox2***** Combobox2.Clear; if ADOQuery1.RecordCount>0 then begin ADOQuery1.First; end; for i:=0 to ADOQuery1.RecordCount-1 do begin //ADOQuery1.First; ComboBox2.Items.Add(ADOQuery1.FieldbyName(ComboBox1.Text).AsString); ADOQuery1.Next; end; Combobox2.ItemIndex := 0; end; //當(dāng)ComboBox1改變時(shí)ComboBox2隨之改變 procedure TForm1.ComboBox1Change(Sender: TObject); var i : integer; begin Combobox2.Clear; if ADOQuery1.RecordCount>0 then begin ADOQuery1.First; end; for i:=0 to ADOQuery1.RecordCount-1 do begin //ADOQuery1.First; ComboBox2.Items.Add(ADOQuery1.FieldbyName(ComboBox1.Text).AsString); ADOQuery1.Next; end; Combobox2.ItemIndex := 0; end; end |
聯(lián)系客服