我想在StringGrid的一个cell中显示上下两行数据,
 形如:
   aaa
   bbb
请问如何实现?
谢谢!

解决方案 »

  1.   

    procedure TListGrid.DrawCell(ACol, ARow: Integer; ARect: TRect;
                                 AState: TGridDrawState);
    var
    r: TRect;
    cp: String;
    begin
      inherited DrawCell(ACol, ARow, ARect, AState);
      if not (csDesigning in ComponentState) then 
      begin
        if gdSelected in AState then
        begin
          EditList.Items.Text:= Cells[ACol, ARow];
          EditList.SetBounds(arect.Left ,
                             arect.Top ,
                             arect.Right - arect.Left ,
                             arect.Bottom - arect.Top );
          EditList.Show;
          EditList.SetFocus;
          EditList.Refresh;
        end else
        begin
          r:= arect;
          cp:= Cells[ACol, ARow];
          canvas.FillRect(R);
          DrawText(Canvas.Handle,pchar(cp),Length(cp), r, DT_Center);
        end;
      end;
    end;
    ===========================================================================
    如果可以请支持一下
    http://expert.csdn.net/Expert/topic/2308/2308724.xml?temp=.1731226
      

  2.   

    unit UListGrid;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls, Grids, ComCtrls, ToolWin;
    type
      TUpdateEven = procedure(Sender: TObject; Value: TStrings; acol, arow: integer ;
                              var Allow: Boolean) of Object;  TListGrid = Class(TStringGrid)
      Private
        EditList: TListBox;
        FOnAdd: TUpdateEven;
        FOnDelete: TUpdateEven;
        FOnRescind: TUpdateEven;
        function CreateEditor: TListBox;
        procedure WMSetFocus(var Msg: TWMSetFocus); message WM_SETFOCUS;
        procedure Adjust(ARow: integer);
      protected
        procedure DrawCell(ACol, ARow: Longint; ARect: TRect;
          AState: TGridDrawState); override;
      public
        constructor Create(AOwner: TComponent); override;
        destructor Destroy; override;    procedure Add;
        procedure Delete;
        procedure Rescind;
      published
        property OnAdd: TUpdateEven read FOnAdd write Fonadd;
        property OnDelete: TUpdateEven read FOnDelete write FOnDelete;
        property OnRescind: TUpdateEven read FOnRescind write FOnRescind;
      end;procedure Register;implementationuses UDialogAdd;{ TListGrid }procedure Register;
    begin
      RegisterComponents('Rock', [TListGrid]);
    end;procedure TListGrid.Add;
    var
      ff: TDialogAdd;
      value: String;
      Allow: Boolean;
      ValueList: TStrings;
      i: integer;
    begin
      ff:= TDialogAdd.Create(self); //创建录入对话框
      ValueList:= TStringList.Create;         //创建列表
      if ff.ShowModal = mrCancel then exit; //显示录入对话框
      ValueList.Text:= ff.Memo1.Text;         //得到用户录入的内容
      allow:= true;                           //初始化变量  if assigned(FOnAdd) then            //检测控件事件
        FOnAdd(self, ValueList, col, row, allow);  //调用控件事件
      if allow then                                //用户允许操作继续
      begin                                        //添加数据
        if ValueList.Count = 0 then exit;
        for i:= 0 to ValueList.Count - 1 do
        begin
          if Cells[col, row] = '' then
            Cells[col, row]:= ValueList.Strings[i] else
            Cells[col, row]:= Cells[col, row] + #13 + ValueList.Strings[i];
        end;
      end;                                         //释放列表
      ValueList.Free;                              //调整行高
      Adjust(row);
    end;procedure TListGrid.Adjust(ARow: integer);
    var
      LineCount, i: integer;
      SList: TStrings;
      RowHeight: integer;
    begin
      LineCount:= 0;
      SList:= TStringlist.Create;
      try
        for i:= 1 to ColCount - 1 do
        begin
          SList.Text:= Cells[i, ARow];
          if SList.Count > LineCount then
            LineCount:= SList.Count;
        end;
        RowHeight:= (editlist.ItemHeight) * LineCount + 1;
        if RowHeight > ClientHeight div 2 then
          RowHeight:= (ClientHeight div 2 div editlist.ItemHeight) * editlist.ItemHeight;
        if RowHeight > DefaultRowHeight then
          RowHeights[Arow]:= RowHeight else
          RowHeights[Arow]:= DefaultRowHeight;
      finally
        SList.Free;
      end;
    end;constructor TListGrid.Create(AOwner: TComponent);
    begin
      inherited Create(AOwner);
      EditList:= CreateEditor;
      self.Options:= self.Options - [goRangeSelect];
    end;function TListGrid.CreateEditor: TListBox;  //创建编辑区
    begin
      Result:= TListBox.Create(self);
      with Result do
      begin
        BorderStyle:= bsNone;
        ParentColor:= false;
        ParentCtl3D:= false;
        Parent:= self;
        MultiSelect:= true;
        Visible:= false;
        Height:= 0;
      end;
    end;procedure TListGrid.Delete;
    var
      ValueList: TStrings;    //最终值的列表
      DeleteList: TStrings;   //删除值的列表
      i: integer;
      allow: boolean;
    begin
      ValueList:= TStringList.Create;   //创建最终值的列表
      DeleteList:= TStringList.Create;  //创建删除值的列表
      for i:= 0 to EditList.Items.Count - 1 do  //填充个列表
      begin
        if EditList.Selected[i] then
          DeleteList.Add(EditList.Items.Strings[i]) else
          ValueList.Add(EditList.Items.Strings[i]);
      end;
      allow:= true;
      if assigned(FOnDelete) then       //调用事件
        FOnDelete(self, DeleteList, col, row, allow);
      if allow then                     //用户允许操作继续
      begin
        if ValueList.Count > 0 then     //将值回填到表格
        for i:= 0 to ValueList.Count - 1 do
        begin
          if i = 0 then
            Cells[col, row]:= ValueList.Strings[i] else
            Cells[Col, row]:= Cells[col, row] + #13 + ValueList.Strings[i];
        end else
        Cells[col, row]:= '';
        Adjust(row);                    //调整行高
      end;
      ValueList.Free;                   //释放列表
      DeleteList.Free;
    end;destructor TListGrid.Destroy;
    begin
      if EditList <> nil then
        EditList.Free;  inherited Destroy;
    end;procedure TListGrid.DrawCell(ACol, ARow: Integer; ARect: TRect;
                                 AState: TGridDrawState);
    var
    r: TRect;
    cp: String;
    begin
      inherited DrawCell(ACol, ARow, ARect, AState);
      if not (csDesigning in ComponentState) then 
      begin
        if gdSelected in AState then
        begin
          EditList.Items.Text:= Cells[ACol, ARow];
          EditList.SetBounds(arect.Left ,
                             arect.Top ,
                             arect.Right - arect.Left ,
                             arect.Bottom - arect.Top );
          EditList.Show;
          EditList.SetFocus;
          EditList.Refresh;
        end else
        begin
          r:= arect;
          cp:= Cells[ACol, ARow];
          canvas.FillRect(R);
          DrawText(Canvas.Handle,pchar(cp),Length(cp), r, DT_Center);
        end;
      end;
    end;procedure TListGrid.Rescind;
    var
      ValueList: TStrings;    //最终值的列表
      DeleteList: TStrings;   //删除值的列表
      i: integer;
      allow: boolean;
    begin
      ValueList:= TStringList.Create;   //创建最终值的列表
      DeleteList:= TStringList.Create;  //创建删除值的列表
      for i:= 0 to EditList.Items.Count - 1 do  //填充个列表
      begin
        if EditList.Selected[i] then
          DeleteList.Add(EditList.Items.Strings[i]) else
          ValueList.Add(EditList.Items.Strings[i]);
      end;
      allow:= true;
      if assigned(FOnRescind) then       //调用事件
        FOnRescind(self, DeleteList, col, row, allow);
      if allow then                     //用户允许操作继续
      begin
        if ValueList.Count > 0 then     //将值回填到表格
        for i:= 0 to ValueList.Count - 1 do
        begin
          if i = 0 then
            Cells[col, row]:= ValueList.Strings[i] else
            Cells[Col, row]:= Cells[col, row] + #13 + ValueList.Strings[i];
        end else
        Cells[col, row]:= '';
        Adjust(row);                    //调整行高
      end;
      ValueList.Free;                   //释放列表
      DeleteList.Free;
    end;
    procedure TListGrid.WMSetFocus(var Msg: TWMSetFocus);
    begin
      inherited;
    end;end.
    全部内容
      

  3.   

    楼上的是指在CELL中画一个控件吧,楼主的问题这样解决就可以了,在STRINGGRID的ONDrawCell事件中写入以下语句
    if( acol=1) and (arow=1) then
    begin
     stringgrid1.Canvas.Font.Size :=2;
     stringgrid1.Canvas.Font.color :=clgreen;
     stringgrid1.Canvas.TextOut(rect.Left,rect.Top,'aaa');
     stringgrid1.Canvas.Font.color :=clred;
     stringgrid1.Canvas.TextOut(rect.Left,rect.Top+12,'bbb');
    end;
      

  4.   

    提供另一种思路,嵌入listbox或memo.
      

  5.   

    楼上面的代码是指在CELL中画一个控件吧,楼主的问题这样解决就可以了,在STRINGGRID的ONDrawCell事件中写入以下语句
    if( acol=1) and (arow=1) then
    begin
     stringgrid1.Canvas.Font.Size :=2;
     stringgrid1.Canvas.Font.color :=clgreen;
     stringgrid1.Canvas.TextOut(rect.Left,rect.Top,'aaa');
     stringgrid1.Canvas.Font.color :=clred;
     stringgrid1.Canvas.TextOut(rect.Left,rect.Top+12,'bbb');
    end;
      

  6.   

    上面的代码是一个封装好的控件。主要作用是实现cell的多行显示,内嵌list是为了实现cell内行数据的删除和编辑。如果不需要这些功能的话可以在ondrawcell中写
    r:= arect;
    cp:= Cells[ACol, ARow];
    canvas.FillRect(R);
    DrawText(Canvas.Handle,pchar(cp),Length(cp), r, DT_Center);
    来输出文本。===========================================================================
    如果可以请支持一下
    http://expert.csdn.net/Expert/topic/2308/2308724.xml?temp=.1731226