Delphi中在ComboBox中添加数据库中字段
// var GridName:TcxGridDBTableView;
with GridName.DataController.DataSource.DataSet do
For J:=0 to GridName.Columncount-1 do
begin
feildName:= Trim(GridName.Columns[J].DataBinding.FieldName);
combobox1.Items.Add(feildName);
end;
end;
with ? dataset ? do begin ? ? first; ? ? while ? not ? eof ? do ? ? begin ? ? ? ? combobox.items.add( ? FieldByName( '学号 ').asString); ? ? ? ? next; ? ? end; end;
你把这段代码放再Form的Create或者Show里面执行就行
|
with ? dataset ? do begin ? ? first; ? ? while ? not ? eof ? do ? ? begin ? ? ? ? combobox.items.add( ? FieldByName( '学号 ').asString); ? ? ? ? next; ? ? end; end;
你把这段代码放再Form的Create或者Show里面执行就行
|
?
adoquery1.Active:=false;
? adoquery1.SQL.Clear;
? adoquery1.SQL.Add('select * from A');
? adoquery1.Active:=true;
? adoquery1.First;
while not adoquery1.Eof do
? begin
?? combobox1.Items.Add(adoquery1.fieldByName('C').AsString);
?? adoquery1.Next;
? end;
adoquery1.First;
while not adoquery1.Eof do
begin
combobox1.Items.Add(adoquery1.FieldByName('列名').AsString);
adoquery1.Next;
end;
combobox1.ItemIndex := 0;
with ? dataset ? do
begin
? ? first;
? ? while ? not ? eof ? do
? ? begin
? ? ? ? combobox.items.add( ? FieldByName( '学号 ').asString);
? ? ? ? next;
? ? end;
end;
你把这段代码放再Form的Create或者Show里面执行就行
=============================================================
1.在Form上添加ComboBox1,ADOQuery1,ADOConnection1(可不要)
2.连接好数据库,南山古桃(nsgtao) 的数据库中有中有表biao1,有字段name
3.运行下面的程序,ComboBox1中会出现name下对应的东东
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日更新下面内容
添加数据库中字段内容到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并有数据表,把组件属性设置完毕->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;<