TStringGrid fixedRow highlight lost

When I turn off default drawing for a TStringGrid, I lose the fixed Row highlighting (on the first row in this case).

I needed to turn default drawing off as I want to use the TA_CENTER function on the headings, and TA_RIGHT on some of the columns. If I don't turn it off, the data appear twice in each column.

Can I set the background color in the DrawCell function; or is there some other way to solve this problem?
Who is Participating?
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.

Pierre CorneliusCommented:
This example shows how to keep fixed row/column colors and right align and vertical center align.
It also maintains highlighting of the selected cell.
Note StringGrid1.DefaultDrawing is false.

procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
var fmt: cardinal;
  with TStringGrid(Sender) do
    if (gdFixed in State)
      then begin
        Canvas.Brush.Color:= FixedColor;
        Canvas.Font.Style:= [fsBold];
      else begin
        if (gdFocused in State) then
          Canvas.Brush.Color:= clHighlight;
          Canvas.Font.Color:= clHighlightText;
        else begin
          Canvas.Brush.Color:= Color;
          Canvas.Font.Color:= Font.Color;
          Canvas.Font.Style:= [];


    fmt:= Windows.DT_RIGHT or windows.DT_VCENTER or Windows.DT_SINGLELINE;

    windows.DrawText(canvas.Handle, Cells[ACol, ARow], length(Cells[ACol, ARow]), Rect, fmt);

Open in new window


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
GrahamDLovellAuthor Commented:
I haven't implemented your full solution yet, but I have taken your canvas.brush.color settings, which has put me on the right track.

Thanks again. The TStringGrid provides great functionality, but it takes a bit of work, and I can see that there is a learning curve I haven't quite mastered yet.
Pierre CorneliusCommented:
Glad i could be of assitance. Good luck.
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.