Link to home
Start Free TrialLog in
Avatar of LeTay
LeTay

asked on

Center text in a TStringGrid cell

Is it possible (and how) to center a text in TStringGrid cell ?
Avatar of bokist
bokist
Flag of Hungary image

Try this solution :

procedure TForm1.MyStringGridDrawCell(Sender: TObject; ACol, ARow: Integer;
          Rect: TRect; State: TGridDrawState);
begin
  with (Sender as TStringGrid), Canvas do
     begin
     TextRect(Rect, Rect.Left + 2, Rect.Top + 2, Cells[Acol, Arow]);
     FrameRect(Rect);
     Display(MyStringGrid, MyStringGrid.Cells[ACol, ARow], Rect, taCenter); //taLeftJustify, taRightJustify
  end;
Avatar of LeTay
LeTay

ASKER

The Display (..) does not compile (undeclared identifier)
Which version of delphi are you using?
Example works with D7.
Avatar of Geert G
have you tried a component like devexpress quantumgrid  ?

it might be expensive, but it would allow you to set the text align = alCenter
for a column
Avatar of LeTay

ASKER

Delphi XE2
Avatar of LeTay

ASKER

Maybe some unit is missing in my uses clause ?
Here is my uses clause :
uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Grids, StdCtrls, ExtCtrls, Buttons, Db, DBTables, Printers, ComCtrls;
Avatar of LeTay

ASKER

I adapted but it does not compile
Can You click on that Display in your environment ?
It will show you the Display' unit
Thanks
ASKER CERTIFIED SOLUTION
Avatar of bokist
bokist
Flag of Hungary image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of LeTay

ASKER

Many  many thanks
You are welcome...
Thank's for points and grading :)