Center text in a TStringGrid cell

Is it possible (and how) to center a text in TStringGrid cell ?
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Try this solution :

procedure TForm1.MyStringGridDrawCell(Sender: TObject; ACol, ARow: Integer;
          Rect: TRect; State: TGridDrawState);
  with (Sender as TStringGrid), Canvas do
     TextRect(Rect, Rect.Left + 2, Rect.Top + 2, Cells[Acol, Arow]);
     Display(MyStringGrid, MyStringGrid.Cells[ACol, ARow], Rect, taCenter); //taLeftJustify, taRightJustify
LeTayAuthor Commented:
The Display (..) does not compile (undeclared identifier)
Which version of delphi are you using?
Example works with D7.
Angular Fundamentals

Learn the fundamentals of Angular 2, a JavaScript framework for developing dynamic single page applications.

Geert GOracle dbaCommented:
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
LeTayAuthor Commented:
Delphi XE2
LeTayAuthor Commented:
Maybe some unit is missing in my uses clause ?
Here is my uses clause :
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Grids, StdCtrls, ExtCtrls, Buttons, Db, DBTables, Printers, ComCtrls;
LeTayAuthor Commented:
I adapted but it does not compile
Can You click on that Display in your environment ?
It will show you the Display' unit
Sorry, I just found this in my project :

procedure Display(StringGrid: TStringGrid; const S: string; Rect: TRect; Alignment: TAlignment);
   Formats: array[TAlignment] of Word = (DT_LEFT, DT_RIGHT, DT_CENTER);

   procedure WriteText(StringGrid: TStringGrid; ACanvas: TCanvas; const ARect: TRect;
                      const Text: string; Format: Word);
      DX, DY : integer;
      S : array[0..255] of Char;
      DX := 2;
      DY := 2;
      with StringGrid, ACanvas, ARect do
           case DefaultRowHeight of
                    1..19 : DY := 2;
                        20 : DY := 3;
                  21..25 : DY := 4;
           case Format of
                  DT_LEFT  : ExtTextOut(Handle, Left + DX, Top + DY,
                                     ETO_OPAQUE or ETO_CLIPPED, @ARect, StrPCopy(S, Text), Length(Text), nil);

                  DT_RIGHT : ExtTextOut(Handle, Right - TextWidth(Text) - 3, Top + DY,
                                       ETO_OPAQUE or ETO_CLIPPED, @ARect, StrPCopy(S, Text), Length(Text), nil);

                DT_CENTER: ExtTextOut(Handle, Left + (Right - Left - TextWidth(Text)) div 2,Top + DY,
                                       ETO_OPAQUE or ETO_CLIPPED, @ARect, StrPCopy(S, Text), Length(Text), nil);

    WriteText(StringGrid, StringGrid.Canvas, Rect, S, Formats[Alignment]);

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
LeTayAuthor Commented:
Many  many thanks
You are welcome...
Thank's for points and grading :)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.