DBGrideh下拉框如何设置,下拉内容动态显示某个字段的所有值,谢谢啊 
pickList,keyList 是手动输入的啊。我是新手,不明白怎么设置.设置lookup字段详细一点。
不太懂delphi,时间又急。

解决方案 »

  1.   

    参考:unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, DB, ADODB, Grids, DBGrids, StdCtrls, DBCtrls;type
      TForm1 = class(TForm)
        DBGrid1: TDBGrid;
        ADOConnection1: TADOConnection;
        ADOQuery1: TADOQuery;
        DataSource1: TDataSource;
        DBComboBox1: TDBComboBox;
        SaveDialog1: TSaveDialog;
        procedure DBGrid1TitleClick(Column: TColumn);
        procedure DBGrid1DrawDataCell(Sender: TObject; const Rect: TRect;
          Field: TField; State: TGridDrawState);
        procedure DBGrid1KeyPress(Sender: TObject; var Key: Char);
        procedure DBGrid1ColExit(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}procedure TForm1.DBGrid1TitleClick(Column: TColumn);
    var
      x: Integer;
      TheField: string;
    begin
      x := column.Index;
      TheField := self.ADOQuery1.Fields[x].FieldName;
      self.ADOQuery1.Close;
      self.ADOQuery1.SQL.Clear;
      self.ADOQuery1.SQL.Text := 'select * from Tb1 order by '+ TheField;
      self.ADOQuery1.Open;
    end;procedure TForm1.DBGrid1DrawDataCell(Sender: TObject; const Rect: TRect;
      Field: TField; State: TGridDrawState);
    begin
      if (gdFocused in State) then
      begin  
        if (Field.FieldName = DBComboBox1.DataField) then
        begin
          self.DBComboBox1.Left := Rect.Left + self.DBGrid1.Left;
          self.DBComboBox1.Top := Rect.top + self.DBGrid1.Top;
          self.DBComboBox1.Width := Rect.Right - Rect.Left;
          DBComboBox1.Height := Rect.Bottom - Rect.Top;
          self.DBComboBox1.Visible := True;
        end;
      end;
    end;procedure TForm1.DBGrid1KeyPress(Sender: TObject; var Key: Char);
    begin
    if (key <> chr(9)) then
      begin
        if (DBGrid1.SelectedField.FieldName =DBComboBox1.DataField) then
        begin
          DBComboBox1.SetFocus;
          SendMessage(DBComboBox1.Handle,WM_Char,word(Key),0);
        end;
      end;  if key = #13 then     //enter 替代Tab
        if not(ActiveControl is TDBGrid) then
        begin
          key := #0;
          perform(WM_NEXTDLGCTL,0,0);
        end
        else
          if (ActiveControl is TDbGrid) then
            with TDBGrid(ActiveControl) do
              if selectedindex <(fieldcount - 1) then
                selectedindex := selectedindex + 1
              else
                selectedindex := 0;end;procedure TForm1.DBGrid1ColExit(Sender: TObject);
    begin
      If DBGrid1.SelectedField.FieldName = DBComboBox1.DataField then
        begin
          DBComboBox1.Visible := false;
        end;
    end;end.
      

  2.   

    卡庙厉害.呵呵
    其实 PickList,keyList 对于静态数据,在界面加载时动态加载后效率是挺高的.