unit Unit1;interfaceuses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, DBCtrls, Grids, DBGrids, DB, DBTables;type
  TForm1 = class(TForm)
    Table1: TTable;
    DataSource1: TDataSource;
    DBGrid1: TDBGrid;
    DBComboBox1: TDBComboBox;
    DBComboBox2: TDBComboBox;
    DBComboBox3: TDBComboBox;
    procedure DBGrid1DrawDataCell(Sender: TObject; const Rect: TRect;
      Field: TField; State: TGridDrawState);
    procedure DBGrid1ColExit(Sender: TObject);
    procedure DBGrid1KeyPress(Sender: TObject; var Key: Char);
  private
    { Private declarations }
  public
    { Public declarations }
  end;var
  Form1: TForm1;implementation{$R *.dfm}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
       DBCombobox1.Left :=Rect.Left + DBgrid1.Left;
       DBCombobox1.Top := Rect.Top + DBgrid1.Top;
       DBCombobox1.Width := Rect.Right - Rect.Left;
       DBCombobox1.Visible :=True;
     end;
     if (Field.FieldName = DBCombobox2.DataField) then
     begin
       DBCombobox2.Left :=Rect.Left + DBgrid1.Left;
       DBCombobox2.Top := Rect.Top + DBgrid1.Top;
       DBCombobox2.Width := Rect.Right - Rect.Left;
       DBCombobox2.Visible :=True;
     end;     if (Field.FieldName = DBCombobox3.DataField) then
     begin
       DBCombobox3.Left :=Rect.Left + DBgrid1.Left;
       DBCombobox3.Top := Rect.Top + DBgrid1.Top;
       DBCombobox3.Width := Rect.Right - Rect.Left;
       DBCombobox3.Visible :=True;
     end;
  end;
end;procedure TForm1.DBGrid1ColExit(Sender: TObject);
begin
  if DBGrid1.SelectedField.FieldName = DBCombobox1.DataField then
  begin
    DBCombobox1.Visible := false;
  end;
  if DBGrid1.SelectedField.FieldName = DBCombobox2.DataField then
  begin
    DBCombobox2.Visible := false;
  end;
  if DBGrid1.SelectedField.FieldName = DBCombobox3.DataField then
  begin
    DBCombobox3.Visible := false;
  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;   if (DBGrid1.SelectedField.FieldName = DBCombobox2.DataField ) then
   begin
     DBCombobox2.SetFocus;
     SendMessage(DBCombobox1.Handle,WM_CHAR,word(Key),0);
   end;   if (DBGrid1.SelectedField.FieldName = DBCombobox3.DataField ) then
   begin
     DBCombobox3.SetFocus;
     SendMessage(DBCombobox1.Handle,WM_CHAR,word(Key),0);
   end;
  end;
end;end.

解决方案 »

  1.   

    DBGrid里的ComboBox不是画上去的。
    DBGrid里点鼠标右键,选“Columns editor...”,鼠标右键,选“Add All Fields”加入所有字段。点你要加ComboBox的字段,在左边的Object Inspector中找到PickList属性。填入你想出现在ComboBox中的项目,就象填普通ComboBox的items属性一样。程序运行后,在设置了pickList属性的字段上双击鼠标,就会出现一个ComboBox
      

  2.   

    procedure TForm1.FormCreate(Sender: TObject);
    begin
      DBGrid1.Columns[0].PickList.Text := 'line1'#13#10'line2'#13#10;
    end;
      

  3.   

    在query或Clientdataset加入一个下拉列表的LookUp字段,将这个新加的字段作为dbgrid的第一个字段即可。
      

  4.   

    伴水也来了啊,那我也玩一玩吧,呵呵,试一试了;
    颜色也可以改变,呵呵,//可以改变DBGrid里的颜色的,不同的记录有不同的颜色
    ------------------------------------------------------------
    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls, DBCtrls, DB, DBTables, Grids, DBGrids;type
      TForm1 = class(TForm)
        DataSource1: TDataSource;
        DBGrid1: TDBGrid;
        Table1: TTable;
        DBComboBox1: TDBComboBox;
        procedure DBGrid1DrawDataCell(Sender: TObject; const Rect: TRect;
          Field: TField; State: TGridDrawState);
        procedure DBGrid1ColExit(Sender: TObject);
        procedure DBGrid1KeyPress(Sender: TObject; var Key: Char);
        procedure DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
          DataCol: Integer; Column: TColumn; State: TGridDrawState);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}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
          DBComboBox1.Left := Rect.Left + DBGrid1.Left;
          DBComboBox1.Top := Rect.Top + DBGrid1.top;
          DBComboBox1.Width := Rect.Right - Rect.Left;
          DBComboBox1.Height := Rect.Bottom - Rect.Top;
          DBComboBox1.Visible := True;
        end;
      end;
    end;procedure TForm1.DBGrid1ColExit(Sender: TObject);
    begin
      If DBGrid1.SelectedField.FieldName = DBComboBox1.DataField then
      begin
        DBComboBox1.Visible := false;
      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;
    end;procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
      DataCol: Integer; Column: TColumn; State: TGridDrawState);
    begin
      if Table1.FieldByName('SIZE').AsInteger = 2 then
      begin
        DBGrid1.Canvas.Font.Color:=clred;
        DBGrid1.Canvas.Brush.color:=clyellow;
      end else
      begin
        DBGrid1.Canvas.Font.Color:=clblue;
        DBGrid1.Canvas.Brush.color:=clBtnFace;
      end;
      DBGrid1.DefaultDrawColumnCell(rect,datacol,column,state);
    end;end.
    -----------------------------------------------------