Board index » delphi » Change color of specific cell in TStringGr

Change color of specific cell in TStringGr

In article 2...@ixnews2.ix.netcom.com, jh...@ix.netcom.com (Jae-Hun Lee ) writes:

Quote
>Anyone knows how to do this ?
>PLEASE HELP !

>I am trying to change the color of specific cell in TStringGrid.
>Whenever I change the color of TStringGrid.Color, then the whole thing
>(TStringGrid) is changed to the given color.

>If there is no way for doing this, are there any ways to change the
>font of the text only for the specific cell ?

This is off the top of my head:

1.  You must set the DrawAutomatic property to False.
2.  You must create a method for the OnDrawCell event.

The OnDrawCell event would look something like:

  var
    Text: array[0..255] of Char;
  begin
  StrPCopy (Text, StringGrid1.Cells[Row, Col]);
  if Col = 2 then
    StringGrid1.Canvas.Brush.Color := clYellow
  else
    StringGrid1.Canvas.Brush.Color := clWhite;
  ExTextOut (..., Rect.Left + 2, Rect.Bottom + 2, ..., Text, StrLen (Text));
  end;

This will draw the third column yellow and all others
white.

Again the code and property names are off the top of my
head.  If you have the VCL source, then take a look in
the GRIDS.PAS file and you will see how the default
OnDrawCell event uses the ExTextOut() function.  You
could take the code from there and paste it into your
source and then add the test for what color to draw the
grid with.

Best regards,
Michael Vincze
m...@asd470.dseg.ti.com

 

Re:Change color of specific cell in TStringGr


Quote
m...@dseg.ti.com (Michael Vincze) wrote:
>In article 2...@ixnews2.ix.netcom.com, jh...@ix.netcom.com (Jae-Hun Lee ) writes:
>>Anyone knows how to do this ?
>>PLEASE HELP !

>>I am trying to change the color of specific cell in TStringGrid.
>>Whenever I change the color of TStringGrid.Color, then the whole thing
>>(TStringGrid) is changed to the given color.

>>If there is no way for doing this, are there any ways to change the
>>font of the text only for the specific cell ?

>2.  You must create a method for the OnDrawCell event.

>The OnDrawCell event would look something like:

Try this from PC Techniques Oct/Nov 95 page 86

procedure TfmDesktop.DBGrid1DrawDataCell(Sender: TObject;
  const Rect: TRect; Field: TField; State: TGridDrawState);
  { see PC Techniques Oct/Nov 95 page 86 }
begin
  if {your test goes here} then begin
    DBGrid1.Canvas.Font.Color := clRed;
    DBGrid1.DefaultDrawDataCell(Rect, Field, State);
  end;
end;

All other cells remain in the default state.
==
Bob Small

Other Threads