请给点实用性的代码吧!以下是我原来写的:结果主窗体的DBGrid里只出现这2个字段.而且COMBOBOX里还有重复的,我想要显示一条有所有的字段的记录,其中COMBOBOX里没有重复的值 procedure TForm1.FormActivate(Sender: TObject); var i:integer;begin combobox1.Items.Clear; with FrmMain.ADOQuery6 do begin close; SQL.Clear; SQL.Add('select DISTINCT Sire,Proper from unit'); Open; while not Eof do for i:=0 to FrmMain.ADOQuery6.RecordCount - 1 do begin combobox1.Items.Add(FrmMain.ADOQuery6.FieldByName('Sire').AsString); combobox2.Items.Add(FrmMain.ADOQuery6.FieldByName('Proper').AsString); Next; end; end; end;
SQL.Add('select DISTINCT Sire,Proper from unit');//这句话的意思就是你要找Sire,Proper并消除取值重复的行呀~那显示出来的当然就只有这两个字段的信息了
在对combobox中添加时,做个判断,select语句不要加distinct了 procedure TForm1.Button1Click(Sender: TObject); var i:integer; begin for i:=-1 to combobox1.ItemIndex do begin if trim(edit1.Text)<>trim(combobox1.Items.Strings[i+1]) then combobox1.Items.Add(edit1.Text);//把edit.text换成你要的东西,这样不会重复了 end; end;
用distinct函数吧,去重复值的.. select distinct 字段 from 表名
论坛上有设置,对于一个帖子的回复,连续回复不可超过三次汗!~ 把你的 SQL.Add('select DISTINCT Sire,Proper from unit'); 改成 SQL.Add('select * from XXX where (sire in (select distinct sire from unit)) and (proper in (select distinct proper from unit))'); 试试看吧~~
把 for i:=0 to FrmMain.ADOQuery6.RecordCount - 1 do 注掉,另外dbgrid里面的问题楼上说得明白
用Dbcombobox,在子窗口打开的时候再分别插入可选选择的值。这样子窗口打开时,Dbcombobox中显示的是当前记录值,点击选择下拉列表中的值完成修改。给你个过程,有重复值是你没有cbx.Clear的原因,看下面的代码!//添加指定字段到DBComboBox控件 procedure LoadDataToDBCbx(sTable,sField:String;Cbx:TDBComboBox); var sSql,sValue:String; begin sSql:=Format('select distinct(%s) from %s',[sField,sTable]); with dm.ADOQ do begin close; sql.Clear; sql.Text:=sSql; open; if RecordCount<1 then exit; cbx.Clear; first; while not Eof do begin sValue:=Trim(FieldByName(sField).Value); cbx.Items.Add(sValue); next; end; end; end;
procedure TForm1.FormActivate(Sender: TObject);
var i:integer;begin
combobox1.Items.Clear;
with FrmMain.ADOQuery6 do
begin
close;
SQL.Clear;
SQL.Add('select DISTINCT Sire,Proper from unit');
Open;
while not Eof do
for i:=0 to FrmMain.ADOQuery6.RecordCount - 1 do
begin
combobox1.Items.Add(FrmMain.ADOQuery6.FieldByName('Sire').AsString);
combobox2.Items.Add(FrmMain.ADOQuery6.FieldByName('Proper').AsString);
Next;
end;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var i:integer;
begin
for i:=-1 to combobox1.ItemIndex do
begin
if trim(edit1.Text)<>trim(combobox1.Items.Strings[i+1])
then combobox1.Items.Add(edit1.Text);//把edit.text换成你要的东西,这样不会重复了
end;
end;
把你的
SQL.Add('select DISTINCT Sire,Proper from unit');
改成
SQL.Add('select * from XXX where (sire in (select distinct sire from unit)) and (proper in (select distinct proper from unit))');
试试看吧~~
for i:=0 to FrmMain.ADOQuery6.RecordCount - 1 do
注掉,另外dbgrid里面的问题楼上说得明白
重復數據的記錄,要看看完完整整的這條記錄是不是有重復(重復的意思是要看看這兩條是不是完完全全一樣呢)?
procedure LoadDataToDBCbx(sTable,sField:String;Cbx:TDBComboBox);
var
sSql,sValue:String;
begin
sSql:=Format('select distinct(%s) from %s',[sField,sTable]);
with dm.ADOQ do
begin
close;
sql.Clear;
sql.Text:=sSql;
open;
if RecordCount<1 then exit;
cbx.Clear;
first;
while not Eof do
begin
sValue:=Trim(FieldByName(sField).Value);
cbx.Items.Add(sValue);
next;
end;
end;
end;