Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Expand TString grid data row / cell while focusing


This question is not answered. Helpful answers available: 2. Correct answers available: 1.


Permlink Replies: 2 - Last Post: Jun 27, 2016 9:53 AM Last Post By: Remy Lebeau (Te...
karthik thirumo...

Posts: 23
Registered: 9/26/13
Expand TString grid data row / cell while focusing  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jun 27, 2016 4:10 AM
Hi,
I'm using Delphi XE3 . I have TStringGrid in my project. As of now i'm showing whole data in the stringgrid cell (that means , showing all the content of the cell) . but i want to display only single line data & when we focus the cell / row then i need to expand that cell / row to show all the data. when we focus out then it needs to be collapsed. Can anyone please help me to achieve this ??
Lajos Juhasz

Posts: 801
Registered: 3/14/14
Re: Expand TString grid data row / cell while focusing  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jun 27, 2016 7:51 AM   in response to: karthik thirumo... in response to: karthik thirumo...
karthik thirumoorthi wrote:

Hi,
I'm using Delphi XE3 . I have TStringGrid in my project. As of
now i'm showing whole data in the stringgrid cell (that means ,
showing all the content of the cell) . but i want to display only
single line data & when we focus the cell / row then i need to expand
that cell / row to show all the data. when we focus out then it needs
to be collapsed. Can anyone please help me to achieve this ??

This is a very quick and a bit dirty demo, but should give you idea how
to achieve this. I would of course subclass TStringGrid:


.......
 
type
  TForm2 = class(TForm)
    StringGrid1: TStringGrid;
    procedure FormCreate(Sender: TObject);
    procedure StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
    procedure StringGrid1SelectCell(Sender: TObject; ACol, ARow:
Integer;
      var CanSelect: Boolean);
  private
    { Private declarations }
    fSelectedRow: integer;
  public
    { Public declarations }
  end;
 
 
.........
 
 
procedure TForm2.FormCreate(Sender: TObject);
begin
  StringGrid1.cells[1,1]:='First row'+#13#10+'Second row';
  StringGrid1.cells[2,2]:='First row'+#13#10+'Second row'+#13#10+'Third
row';
  StringGrid1.cells[3,3]:='First row'+#13#10+'Second row';
  fSelectedRow:=-1;
end;
 
procedure TForm2.StringGrid1DrawCell(Sender: TObject; ACol, ARow:
Integer;
  Rect: TRect; State: TGridDrawState);
var ltext: string;
     lpDTP :  TDrawTextParams;
begin
  if (Acol>0) or (Arow>0) then
  begin
    StringGrid1.Canvas.FillRect(Rect);
    lText:=Stringgrid1.Cells[Acol, Arow];
    lpDTP.cbSize:=SizeOf(lpDTP);
    lpDTP.uiLengthDrawn:=Length(lText);
     lpDTP.iLeftMargin:=0;
    lpDTP.iRightMargin:=0;
 
 
    DrawTextEx(StringGrid1.Canvas.Handle,PChar(lText), Length(lText),
{}
               rect, DT_WORDBREAK,@lpDTP);
  end;
end;
 
procedure TForm2.StringGrid1SelectCell(Sender: TObject; ACol, ARow:
Integer;
  var CanSelect: Boolean);
var lRect: TRect;
var ltext: string;
    lpDTP :  TDrawTextParams;
    x: integer;
    lminHeight: integer;
begin
  if Arow<>fSelectedRow then
  begin
    if fSelectedRow<>-1 then
 
StringGrid1.RowHeights[fSelectedRow]:=stringgrid1.DefaultRowHeight;
 
    StringGrid1.canvas.Font:=StringGrid1.Font;
    lminHeight:=0;
    for x:=0 to StringGrid1.ColCount-1 do
    begin
      lText:=StringGrid1.Cells[x, Arow];
      lpDTP.cbSize:=SizeOf(lpDTP);
      lpDTP.uiLengthDrawn:=Length(lText);
       lpDTP.iLeftMargin:=0;
      lpDTP.iRightMargin:=0;
 
      lRect:=StringGrid1.CellRect(x, ARow);
 
 
      DrawTextEx(StringGrid1.Canvas.Handle,PChar(lText), Length(lText),
{}
                 lrect, DT_WORDBREAK or DT_CALCRECT,@lpDTP);
      if lRect.Bottom-lRect.Top>lminHeight then
        lminHeight:=lRect.Bottom-lRect.Top;
    end;
    if lMinHeight>0 then
      StringGrid1.RowHeights[arow]:=lminHeight;
    fSelectedRow:=Arow;
  end;
end;
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Expand TString grid data row / cell while focusing  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jun 27, 2016 9:53 AM   in response to: karthik thirumo... in response to: karthik thirumo...
karthik wrote:

i want to display only single line data & when we focus the cell / row
then i need to expand that cell / row to show all the data. when we
focus out then it needs to be collapsed.

You need to owner-draw the cells, then you can draw whatever you want. Use
the OnDrawCell event for that. When drawing a cell, draw the entire data
if that cell is currently selected, otherwise draw the first line instead.

--
Remy Lebeau (TeamB)
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02