有两个字段例如:用户名,姓名
                www1   张三
        yd2    李四
我怎么能在combobox下拉列表中(界面)显示出姓名列,又能在保存时从后台保存到数据库的是
用户名字段,谢谢

解决方案 »

  1.   

    type
      pFields=^TFields;//定义一个记录集
      TFields=Record
        ID,CName,EName:String;
      end;在添加到ComboBox时如下:
    var pF:pFields;
    begin
    New(pF);
    pF.ID:=ADOQuery1.FieldByName('ID').AsString;
    pF.CName:=ADOQuery1.FieldByName('CName').AsString;
    pF.EName:=ADOQuery1.FieldByName('EName').AsString;
    ComboBox1.Items.AddObject(pF.CName,TObject(pF));//ComboBox1.Items.AddObject('要显示的内容',TObject(记录集(不能为nil)));调用时如下:
    begin
      if ComboBox1.ItemIndex<0 then Exit;
      Edit1.Text:=pFields(ComboBox1.Items.Object[ComboBox1.ItemIndex]).EName;
    end;
      

  2.   

    用DbCombox ,可以实现,然后根据index 来查找对应的用户名
      

  3.   

    实际上就象web中的表单的select一样,可以同时保存两个值
    <select name="select">
      <option value="www1">张三</option>
      <option value="yd2">李四</option>
      </select>
    保存时用value
      

  4.   

    回复人: lwgygz(小欣)的問題确實可行. 但簡單問題復雜化其實用TDBLookupComboBox控件解決問題更簡單!
      

  5.   

    先把用户名用一个STRINGLIS保存起来,再
    StrList:=Tstringlist.Create;
    while not adodataset eof do  begin
    begin
    StrList.Add(FieldValues['用户名']);
    end;StrList[ComboBox1.Items.IndexOf(ComboBox1.Text)]
      

  6.   

    ComboBox1的位置要和StrList的位置一一对应,不要搞反了,^_^!
      

  7.   

    要简单一些可以这样:
    1.在combobox里加入"姓名--用户名"
    2.在combobox的onchange事件里取得"用户名"("--"后的字符串可以使用pos取得)并保存
      

  8.   

    lwgygz(小欣)的方法是通用性最强的解法
    但如果搂主只想实现ID与Value对应,可使用以下简单用发var IDArr:Array of string;
        RowCount:integer;
    procedure  Add;
    begin
      SetLength(IDArr, ADOQuery1.RecordCount);
      RowCount := 0;
      while not eof do
      begin
      combobox1.Items.Strings[RowCount]:=ADOQuery1.FieldByName('Name').AsString;
      IDArr[RowCount]:=ADOQuery1.FieldByName('ID').AsString;
      Inc(RowCount);
      Next;
      end;
    end;使用时
      IDArr[combobox1.ItemIndex] 就是你要的ID值
      

  9.   

    lwgygz(小欣)的方法是对象化后扩展性最强的解法
    但如果搂主只想实现ID与Value对应,可使用以下简单用发var IDArr:Array of string;
        RowCount:integer;
    procedure  Add;
    begin
      SetLength(IDArr, ADOQuery1.RecordCount);
      RowCount := 0;
      while not eof do
      begin
      combobox1.Items.Strings[RowCount]:=ADOQuery1.FieldByName('Name').AsString;
      IDArr[RowCount]:=ADOQuery1.FieldByName('ID').AsString;
      Inc(RowCount);
      Next;
      end;
    end;使用时
      IDArr[combobox1.ItemIndex] 就是你要的ID值