怎样把一个表中的一个字段值加入COMBOBOX中,注意这个字段中的值有重复值,重复的只加一个

解决方案 »

  1.   

    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;
      

  2.   

    var str: String;Query1.Open;
    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;
      

  3.   

    while not Query1.Eof do
    begin
      s := Query1.Fieldbyname('field1').AsString;
      if ComboBox1.Items.IndexOf(s) < 0 then
        Combobox1.Items.Add(s);
      next;
    end;
      

  4.   

    我有个函数就是做这个的,给你参考:// 本过程为通用过程,用于初始化ComboBox的Items.
    // 参数说明:
    // 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'来查询就可以了。
      

  5.   

    这个好办,只是在取字段时用DISTINCT限制即可,代码如下:
    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;
      

  6.   

    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;
      

  7.   

    这个容易,可以采用分组
    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;
      

  8.   

    真是物有所值啊,呵呵;
    Distinct就是取不重复的值的;
    如果你都要显示的话,就用两个Query;
    First;
    Not Eof
    ComboBox.Items.Add(FieldBYName('FieldName').AsString);
    Next;