Delphi Pages Forums  

Go Back   Delphi Pages Forums > Delphi Forum > DB-Aware

Lost Password?

Reply
 
Thread Tools Display Modes
  #1  
Old 12-11-2000, 01:00 PM
N/A
Guest
 
Posts: n/a
Default How to make color row on DBGrig in Delphi5

I need to display row (record) in red or blue or black color depend upon some condition. On same grid I have to have more then one color. Is it possible to do in Delphi 5 using DBGrid?



Thanks
Reply With Quote
  #2  
Old 12-12-2000, 06:07 PM
N/A
Guest
 
Posts: n/a
Default RE: How to make color row on DBGrig in Delphi5

Here is a copy of a technical article (aslo available through the Delphi
Community site) on the subject. Watch how it handles selected versus
non-selected cells.

-------------------------------------------------------------

To do this, create a handler for the OnDrawDataCell event of the TDBGrid.
In this event handler, check various factors and conditionally set the
brush color for the grid's canvas to one color or another. Then, to draw
the cell, call the TDBGrid component's DefaultDrawDataCell method.

The OnDrawDataCell event fires once for each cell drawn. To tell what field
is currently being drawn, inspect the var parameter Field passed to the
event handler. This is a TField type reference and its FieldName property
reveals the drawn field's name.

Use this same Field parameter to determine the field's value when it is the
field you wish to conditionally color. Use TField properties like AsString
and AsInteger to get the field's value for use in comparisons in if..else
constructs.

To determine whether the cell being drawn has focus or is just another
displayed cell, check for the TGridDrawState constant gdFocused in the
State parameter passed to the event handler. You might want to paint the
focused cell and its font with a different color.

When calling the DefaultDrawDataCell method, use the var parameters passed
to the OnDrawDataCell event handler as the arguments for
DefaultDrawDataCell. The passed Rect parameter represents the rectangle in
the TDBGrid component's canvas covered by the cell currently being drawn.
The Field parameter is the field associated with the column, used by
DefaultDrawDataCell to get the text for the cell. And the State parameter
indicates whether the current cell is focused, fixed, selected, or simply a
displayed cell. Set the value of the TDBGrid.Canvas.Brush.Color property to
specify the background color for the cell and the TDBGrid.Canvas.Font.Color
property to indicate the color of the text in the cell. When the
DefaultDrawDataCell method is called, the cell is drawn using these color
settings.

The example below uses the sample Paradox table Orders. Its Terms field is
the one to be conditionally colored. This field contains two values: "FOB"
(which will be colored the custom color clPaleGreen) and "Net 30" (colored
the custom color clPaleRed). This event handler checks for the currently
drawn cell having focus and, if it does, paints its area clBlack and its
font clWhite. All cells that are not conditionally colored and do not have
focus are painted with a background color of clWhite. All cells that do not
have focus have a font color of clBlack.

procedure TForm1.DBGrid1DrawDataCell(Sender: TObject; const Rect: TRect;
Field: TField; State: TGridDrawState);
{ Define custom colors }
const
clPaleGreen = TColor($CCFFCC);
clPaleRed = TColor($CCCCFF);
begin
{ Set default font color }
DBGrid1.Canvas.Font.Color := clBlack;
{ Is current cell the focused cell? }
if (gdFocused in State) then begin
DBGrid1.Canvas.Brush.Color := clBlack;
DBGrid1.Canvas.Font.Color := clWhite;
end
{ Not focused, is it the target field Terms? }
else if (Field.FieldName = 'Terms') then
{ Check field content for one of two possible values }
if (Field.AsString = 'FOB') then
{ If field contains "FOB" paint green }
DBGrid1.Canvas.Brush.Color := clPaleGreen
else
{ If field contains "Net 30" paint red }
DBGrid1.Canvas.Brush.Color := clPaleRed
else
{ Paint non-focused and non-target cells white }
DBGrid1.Canvas.Brush.Color := clWhite;
{ Draw the cell! }
DBGrid1.DefaultDrawDataCell(Rect, Field, State);
end;
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 04:02 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2013, vBulletin Solutions, Inc.