这个容易,可以采用分组 with dataset1 do begin close; sql.Clear ; sql.Add('select field1 from table1 group by field1); open; first; while not eof do begin combobox1.Items.Add(fieldbyname('field1').AsString ); next; end; end; 也可以第一句:select distinct field1 from table1
在ComboBox.Items.Add的时候采用如下语法:var item: string;if ComboBox.Items.IndexOf(item) < 0 then ComboBox.Items.Add(item);在添加Item的时候先判断一下值是否存在
如果Item已加,以下代码是删除的代码,已调试通过:procedure TForm1.BitBtn1Click(Sender: TObject); var I,J: Integer; begin for I := 0 to self.ComboBox1.Items.Count - 1 do begin for J := I+1 to self.ComboBox1.Items.Count - 1 do begin if (self.ComboBox1.Items[J] = self.ComboBox1.Items[I]) then self.ComboBox1.Items.Delete(J); end; end; end;
先排序再去重复比较快: Var Strs: TStringList Prev: String; I: Integer; begin Strs := TStringList.Create; Strs.Text = ComboBox1.Items.Text; Strs.Sort; ComboBox1.Items.Clear; Prev := ''; for I := 0 to Strs.Count - 1 do begin if not AnsiSameStr(Prev, Strs[I]) then begin Prev := Strs[I]; ComboBox1.Items.Add(Prev); end; end; end;
如果要求保持顺序会慢一些: Var Strs: TStringList I: Integer; begin Strs := TStringList.Create; Strs.Text = ComboBox1.Items.Text; ComboBox1.Items.Clear; for I := 0 to Strs.Count - 1 do begin if ComboBox1.IndexOf(Strs[I]) <> -1 then begin ComboBox1.Items.Add(Strs[I]); end; end; end;
combobox.items.clear; Query.close; query.sql.text:='Select distinct Field1 from table1 where...'; query.open; while not query.eof do begin combobox.items.add(query.FindField('Field1').AsString); query.next; end;
with dataset1 do
begin
close;
sql.Clear ;
sql.Add('select field1 from table1 group by field1);
open;
first;
while not eof do
begin
combobox1.Items.Add(fieldbyname('field1').AsString );
next;
end;
end;
也可以第一句:select distinct field1 from table1
3
4 ,6
等等。
也就是一个里放多条数据,我读到,combobox.item 里,变成了,4
5
3
4
6
我第一次从库里也是用 ,DISTINCT ,
ComboBox.Items.Add(item);在添加Item的时候先判断一下值是否存在
var I,J: Integer;
begin
for I := 0 to self.ComboBox1.Items.Count - 1 do
begin
for J := I+1 to self.ComboBox1.Items.Count - 1 do
begin
if (self.ComboBox1.Items[J] = self.ComboBox1.Items[I]) then
self.ComboBox1.Items.Delete(J);
end;
end;
end;
Var
Strs: TStringList
Prev: String;
I: Integer;
begin
Strs := TStringList.Create;
Strs.Text = ComboBox1.Items.Text;
Strs.Sort;
ComboBox1.Items.Clear;
Prev := '';
for I := 0 to Strs.Count - 1 do
begin
if not AnsiSameStr(Prev, Strs[I]) then
begin
Prev := Strs[I];
ComboBox1.Items.Add(Prev);
end;
end;
end;
Var
Strs: TStringList
I: Integer;
begin
Strs := TStringList.Create;
Strs.Text = ComboBox1.Items.Text;
ComboBox1.Items.Clear;
for I := 0 to Strs.Count - 1 do
begin
if ComboBox1.IndexOf(Strs[I]) <> -1 then
begin
ComboBox1.Items.Add(Strs[I]);
end;
end;
end;
Query.close;
query.sql.text:='Select distinct Field1 from table1 where...';
query.open;
while not query.eof do
begin
combobox.items.add(query.FindField('Field1').AsString);
query.next;
end;
plainsong(短歌) 的方法也可行。
已经解决了,所以还是给分,每人10分,不好意思,分太少,
L: TStringList;
I: Integer;
begin
L := TStringList.Create;
with L do
try
Duplicates := dupIgnore;
Sorted := True;
Assign(ListBox1.Items);
ListBox1.Items.Clear;
ListBox1.Items.Assign(L);
finally
Free;
end;