代码如下:
//自定义表格
unit Unit2;interfaceuses classes, grids, StdCtrls;  TCustomGrid = class(TStringGrid)
  private
    FComboBox : TComboBox;
    FPickList : TStrings;
  protected
    procedure DrawCell(ACol, ARow: Longint; ARect: TRect;
      AState: TGridDrawState); override;
    procedure InitialComboBox(ARect: TRect);
    procedure OnCustomSelectCell(Sender: TObject; ACol, ARow: Longint; var
        CanSelect: Boolean); virtual;
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;    property PickList : TStrings read FPickList write FPickList;
  end;
{ TCustomGrid }constructor TCustomGrid.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  DefaultColWidth := 64;
  DefaultRowHeight := 18;
  Options := Options + [goEditing];  FComboBox := TComboBox.Create(self);  FPickList := TStringList.Create;  OnSelectCell := OnCustomSelectCell;
end;destructor TCustomGrid.Destroy;
begin
  FPickList.Free;  FComboBox.Free;
  inherited;
end;procedure TCustomGrid.DrawCell(ACol, ARow: Integer; ARect: TRect;
  AState: TGridDrawState);
begin
  inherited;
end;procedure TCustomGrid.InitialComboBox(ARect: TRect);
begin
  with ARect do
    FComboBox.SetBounds(Left, Top, (Right - Left), (Bottom - Top));
  FComboBox.Parent := self;
  FComboBox.Items.Assign(FPickList);
  FComboBox.Visible := True;
  FComboBox.SetFocus;
end;procedure TCustomGrid.OnCustomSelectCell(Sender: TObject; ACol, ARow: Longint;
    var CanSelect: Boolean);
var
  R : TRect;
begin
  R := CellRect(ACol, ARow);
  InitialComboBox(R);
end;
end.//应用自定义表格
var
  aGrid : TCustomGrid;
begin
  aGrid := TCustomGrid.Create(self);
  aGrid.SetBounds(20,20,450,300);
  aGrid.Parent := self;  aGrid.PickList.Add('aa');
  aGrid.PickList.Add('bb');
end;

解决方案 »

  1.   

    如果再TCustomGrid.Create中对ComboBox的Items进行操作,会产生一个错误"control '' not has no a parent window",不知道为什么?
      

  2.   

    unit Unit2;interfaceuses classes, grids, StdCtrls, windows, Controls;type
      TCustomGrid = class(TStringGrid)
      private
        FComboBox : TComboBox;
        FPickList : TStrings;
      protected
        procedure DrawCell(ACol, ARow: Longint; ARect: TRect;
          AState: TGridDrawState); override;
        procedure InitialComboBox(ARect: TRect);
        procedure OnCustomSelectCell(Sender: TObject; ACol, ARow: Longint; var
            CanSelect: Boolean); virtual;
      public
        constructor Create(AOwner: TComponent); override;
        destructor Destroy; override;
        property PickList : TStrings read FPickList write FPickList;
      end;implementationconstructor TCustomGrid.Create(AOwner: TComponent);
    begin
      inherited Create(AOwner);
      DefaultColWidth := 64;
      DefaultRowHeight := 18;
      Options := Options + [goEditing];  FComboBox := TComboBox.Create(nil);
      FComboBox.Parent:=TWinControl(AOwner);
      FComboBox.Visible:=false;
      FComboBox.Items.Add('a');  FPickList := TStringList.Create;  OnSelectCell := OnCustomSelectCell;
    end;destructor TCustomGrid.Destroy;
    begin
      FPickList.Free;  //FComboBox.Free;
      inherited;
    end;procedure TCustomGrid.DrawCell(ACol, ARow: Integer; ARect: TRect;
      AState: TGridDrawState);
    begin
      inherited;
    end;procedure TCustomGrid.InitialComboBox(ARect: TRect);
    begin
      with ARect do
        FComboBox.SetBounds(Left+Self.Left, Top+Self.Top, (Right - Left), (Bottom - Top));
      //FComboBox.Parent := self;
      FComboBox.BringToFront;
      FComboBox.Items.Assign(FPickList);
      FComboBox.Visible := True;
      FComboBox.SetFocus;
    end;procedure TCustomGrid.OnCustomSelectCell(Sender: TObject; ACol, ARow: Longint;
        var CanSelect: Boolean);
    var
      R : TRect;
    begin
      R := CellRect(ACol, ARow);
      InitialComboBox(R);
    end;end.