简体   繁体   中英

How to change the colour of the stringgrid particular cell using Delphi XE7

I'm using Delphi XE7 for developing Android application. In that I have used TStringGrid component and then I have used StringGrid.cells[0, 0] := 'Test' And how can I change the Font colour of that particular cell which I have shown in the code. And also I have this sample code, but I can not change the font colour of the particular cell. Please anybody explain me how to change the font colour of the particular cell value. And I'm using Delphi XE7 and I'm targeting Android mobile.

Thanks..

In a FireMonkey TStringGrid there are no per cell styling options. You will either need to use a third party grid control or roll something yourself from TGrid.

You can find plenty of material on the latter on my site at http://monkeystyler.com/guide

At last, I have found the solution which I required. Please follow the Steps. We can able to change the font color in TStringGrid itself, No need to use TGrid. Please follow the below steps.

First assign this in FormCreate event:

  StringGrid1.DefaultDrawing := False;

then write this in StringGrid DrawColumnCell event:

  Canvas.fill.Color := TAlphaColorRec.Green;
  Canvas.FillText(Bounds, (Value.AsString),
    false, 100, [], TTextAlign.taLeading, TTextAlign.taCenter);

Works in XE8 as well for the TStringGrid OnDrawColumnCell event.

Herewith an example that keeps the color on black but sets the font styling to bold. Tip, add 2 pixels padding for the font, from the left margin.

var Rect : TRectF;
begin
  Rect := Bounds;
  Rect.Left := Rect.Left + 2;
  Canvas.Font.Style := [TFontStyle.fsBold];
  Canvas.Fill.Color := TAlphaColorRec.Black;
  Canvas.FillText(Rect, (Value.AsString), false, 100, [], TTextAlign.taLeading, TTextAlign.taCenter);
end;

What I missed in the beginning was not setting the DefaultDrawing to false! After I set that, the event was accepting changes to the Canvas.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM