Link to home
Start Free TrialLog in
Avatar of jaldana
jaldana

asked on

color in a row of a dbgrid

we are using dbgrid's and we need paint diferent rows with different colors how can I do it ?
ASKER CERTIFIED SOLUTION
Avatar of inter
inter
Flag of Türkiye image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of julio011597
julio011597

What about an OwnerDraw'ed DBGrid?
It is not that hard, IMO.
Fine,
Thats one way, however, can not simply compete with RxLib(it takes time I mean). Any way julio is right.
Regards,
Inter
Sure the simplest way of doing something is to use what somebody
else has written, but that dowsn't teach you anything.

I would suggest that you use an OwnerDrawGrid.  All you have to
do is the following:
 Change the DefaultDrawing property of the DBGrid to False

 Add Some code to the OnDrawColumnCell (*Use this one as
 OnDrawDataCell is supposed to obsolete *)
 e.g.
procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
  DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
  if Table1.FieldByName('ItemsTotal').Value > 10000 then
  begin
    // Change the line to be bold for lines with above ^^
    DBGrid1.Canvas.Font.Style := [fsBold];  
    if Column.FieldName = 'ItemsTotal' then
      // Change the Color to red for the ItemsTotalColumn if
      // the prior condition is met
      DBGrid1.Canvas.Font.color := clRed;
  end;

  // Call the default drawing now as all we have done is changed
  // the color of thins.  If we wanted to draw in different
  // styles like 3D tehn we would have to handle that aswell
  DBGrid1.DefaultDrawColumnCell( Rect, DataCol, Column, State );
end;


hope this helps

Scott