怎样把一个表中的一个字段值加入COMBOBOX中,注意这个字段中的值有重复值,重复的只加一个
解决方案 »
- 再次问关于Delphi 6 的TIdTCPClient超时问题。是delphi 6!!!
- 怎样做一个简单的虚拟文件系统
- 求用delphi7设计的socket客户端的方法,并实现通过socket接口发字符串的功能,包括具体源码和编写过程,我是菜鸟,谢谢
- 生成与读取xml
- 求解数据格式
- 哪位高手有空帮我翻译下面藏文内容
- 关于adoquery的问题??
- DefaultExt(缺省后缀)的字符串是怎样的?
- C/S类型地数据库应用客户端软件一运行就非法操作
- 为什么我将OLEContainer中的WORD文档保存到数据库后,下次再从数据库读出来的时候是乱码呀??很急呀!!
- 1stclass3000provcl5.exe的注册码?
- 请问各位帅哥们,有打反恐的吗,都是在那里玩的
begin
close;
sql.Clear ;
sql.Add('select distinct field1 from table1);
open;
first;
while not eof do
begin
combobox1.Items.Add(fieldbyname('field1').AsString );
next;
end;
end;
Query1.First;
while not Query1.Eof do
begin
str := Query1.FieldByName('ColName').AsString;
if ComboBox1.Items.IndexOf(str) < 0 then
ComboBox1.Items.Add(str);
Query1.Next;
end;
begin
s := Query1.Fieldbyname('field1').AsString;
if ComboBox1.Items.IndexOf(s) < 0 then
Combobox1.Items.Add(s);
next;
end;
// 参数说明:
// ComBoBox:需要初始化的选择框
// DataSource: 数据来源
// FieldName:插入ComBoBox的字段名
// FirstItem:插入ComBoBox的第一项即Items[0]的值,当strFirstItem=''时将不插入.
procedure InitComboBox(ComBoBox:TCustomComboBox;
DataSource:TCustomADODataset;
FieldName,FirstItem:String);
begin
if ((DataSource=nil) or (not DataSource.Active)
or ((DataSource.RecordCount=0) and (trim(FirstItem)=''))) then
Exit;
ComBoBox.Items.Clear;
with DataSource do
begin
first;
if trim(FirstItem)<>'' then
ComBoBox.Items.add(FirstItem);
while not EOF do
begin
ComBoBox.Items.add(FieldByName(FieldName).AsString);
Next;
end;
end;
end;
参数DataSource连到你要初始化的数据集即可,如果不希望插入重复值,就按 timelyraining写的SQL语句'select distinct field1 from table1'来查询就可以了。
with dataset1 do
begin
close;
sql.Clear ;
sql.Add('select distinct field1 from table1);
open;
first;
while not eof do
begin
combobox1.Items.Add(fieldbyname('field1').AsString );
next;
end;
end;
begin
close;
sql.Clear ;
sql.Add('select distinct field1 from table1);
open;
first;
while not eof do
begin
combobox1.Items.Add(fieldbyname('field1').AsString );
next;
end;
end;
with dataset1 do
begin
close;
sql.Clear ;
sql.Add('select field1 from table1 group field1);
open;
first;
while not eof do
begin
combobox1.Items.Add(fieldbyname('field1').AsString );
next;
end;
end;
Distinct就是取不重复的值的;
如果你都要显示的话,就用两个Query;
First;
Not Eof
ComboBox.Items.Add(FieldBYName('FieldName').AsString);
Next;