Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can one control font colour for single column/line in a grid? 2

Status
Not open for further replies.

delphiman

Programmer
Dec 13, 2001
422
ZA
The following code relates to the value of qryCBDOC.value in a record. But when the condition is satisfied it results in the font color for the entire column being changed to Red in the grid. Can someone please tell me how I can get the font color to change for only that line (that is to say for only that record) in the grid?


if system.Copy(qryCBDOC.value,1,1) = 'D' then // Change color of font for column 9
begin
grdCB.columns.items[9].Font.Color:= clred;
end;
 
I'm assuming that grdCB is a TDBGrid.

You need to write an OnDrawColumnCell event handler for grdCB.

The OnDrawColumnCell event handler is called for each cell that is drawn.

In the event handler you can choose the font (and brush) colour to use depending on the value of qryCBDOC.

Your event handler could look something like
Code:
procedure TForm1.grdCBDrawColumnCell(Sender: TObject; const Rect: TRect;
  DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
  if system.Copy(qryCBDOC.value,1,1) = 'D' then
    grdCB.Canvas.Font.Color := clRed
  else
    grdCB.Canvas.Font.Color := clBlack;
  grdCB.Canvas.TextRect ( rect, rect.Left, rect.top, qryCBDOC.fields[datacol].AsString );
end;
Note that this is minimal demonstration code. For a professional appearance you will probably want to enhance it so that the text is placed vertically in the middle of the cell and that there is a left hand margin of a few pixels. Other cosmetic details you might consider is how to handle fields which are too wide for the column and so on.

You should think carefully how to structure your code to simplify maintenance. DrawCell type procedures can get rather convoluted.

Note that the use of 'value' in the IF statement is not as efficient as using 'AsString'. I don't know whether that is important for your application.

Check out the Delphi Help for the DefaultDrawing property of TDBGrid and the OnDrawColumnCell event handler.


Andrew
Hampshire, UK
 
Terry maybe you find something of interest in:

How to use multi-colored lines in a grid
faq102-2416

in the FAQ area

Regards




Steven van Els
SAvanEls@cq-link.sr
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top