各位高手好:我把代码改成: procedure TForm1.FormCreate(Sender: TObject); begin query1.close; query1.sql.Clear; DBComboBox1.Items.Clear; query1.SQL.add('select distinct content from DEPTCODE'); query1.open; while not query1.Eof do begin DBComboBox1.Items.Append(query1.fieldbyname('content').AsString); query1.Next; end; dbcombobox1.datafield:='content'; dbcombobox1.datasource:=datasource1; end;现在可以任意选择了,但是程序运行时,我点击其他地方,比如我的Edit控件,DBComboBox就变成了我表中的第一个字段了,这又是为什么?怎么解决?
这是我刚写的一段代码,运行正常.你稍做替换修改一下吧,说白了你犯的错误就是数据库没打开! procedure TForm1.FormCreate(Sender: TObject); var s_Sql_Query:tadoquery; i:integer; begin //打开数据库 if not adoquery1.Active then adoquery1.Open; s_Sql_Query :=tadoquery.Create(self); s_sql_Query.Connection :=ADOConnection1; s_Sql_Query.Close; s_Sql_query.SQL.Clear; s_sql_Query.SQL.Text :='select distinct nation_type from cumm3000'; s_sql_Query.Open; DBComboBox1.Items.Clear; for i :=1 to s_sql_query.RecordCount do begin if s_sql_query.Fields[0].asstring<>'' then DBComboBox1.Items.Add(s_sql_query.Fields[0].asstring); s_sql_Query.Next; end; s_sql_Query.Free;end;
请在adoquery的SQL参数中输入
SELECT * FROM 表名(字段所在表),然后ACTIVE设置为True
begin
query1.Active:=False;
query1.sql.Clear;
query1.SQL.add('select distinct content from DEPTCODE');
query1.Active:=true;
//增加一个datasource控件,然后
datasource1.dataset:=query1;
DBComboBox1.datasource:=datasource1;
DBComboBox1.datafield:='content';
end;
这样dbcombobox的datafield应该可以指定,然后formcreate事件中动态改变query的sql为select distinct content from deptcode
你的//增加一个datasource控件,然后··是什么意思?
TO: larruping(总要把热情渗入^_^) :我按照你的方法做了,和以前一样,可以看见,不能选择!我的QQ:4806144
MSN:[email protected]大家可以加我,我一直在!
也许用QQ和MSN更方便!
高手,进来吧,我在等你们!!
你添加items到dbcombobox的代码没有问题,至于说设定datafield时的错误,可以这样处理:首先把datasource去掉,然后直接在datafield中写入对应字段的名称,再回去设定datasource就可以了。
datasource对应的query的sql属性可以不写任何的sql,写了也没有什么意思。
procedure TForm1.FormCreate(Sender: TObject);
begin
query1.close;
query1.sql.Clear;
DBComboBox1.Items.Clear;
query1.SQL.add('select distinct content from DEPTCODE');
query1.open;
while not query1.Eof do
begin
DBComboBox1.Items.Append(query1.fieldbyname('content').AsString);
query1.Next;
end;
dbcombobox1.datafield:='content';
dbcombobox1.datasource:=datasource1;
end;现在可以任意选择了,但是程序运行时,我点击其他地方,比如我的Edit控件,DBComboBox就变成了我表中的第一个字段了,这又是为什么?怎么解决?
procedure TForm1.FormCreate(Sender: TObject);
var
s_Sql_Query:tadoquery;
i:integer;
begin
//打开数据库
if not adoquery1.Active then
adoquery1.Open; s_Sql_Query :=tadoquery.Create(self);
s_sql_Query.Connection :=ADOConnection1;
s_Sql_Query.Close;
s_Sql_query.SQL.Clear;
s_sql_Query.SQL.Text :='select distinct nation_type from cumm3000';
s_sql_Query.Open;
DBComboBox1.Items.Clear;
for i :=1 to s_sql_query.RecordCount do
begin
if s_sql_query.Fields[0].asstring<>'' then
DBComboBox1.Items.Add(s_sql_query.Fields[0].asstring);
s_sql_Query.Next;
end;
s_sql_Query.Free;end;