Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 660
  • Last Modified:

dbgrid with different colours in rows

This surely is an easy question, but I can't manage it:
How can I make that in a dbgrid a row has a differnt colour depending of the value in a recordfield of that row.
For example:
If the fieldvalue of column[5] is null, all the entries in that row must be drawn in red ?
Thank you.
0
SCHUCla74
Asked:
SCHUCla74
1 Solution
 
LukA_YJKCommented:
0
 
MotazCommented:
Put this code in dbGrid OnDrawDataCell:

procedure TfmMain.DBGrid1DrawDataCell(Sender: TObject; const Rect: TRect;
  Field: TField; State: TGridDrawState);
begin

  if dbGrid1.DataSet.Fields[4].IsNull then
    dbGrid1.Canvas.Brush.Color:= clRed
  else
    dbGrid1.Canvas.Brush.Color:= clWhite
     
  dbGrid1.Canvas.TextRect(Rect, Rect.Left,  Rect.Top, Field.AsString);
 
end;
0
 
da-vinciCommented:
OnDrawDataCell is not recommended anymore, this is what Borland says: "OnDrawDataCell is obsolete and included for backward compatibility. Instead, write an OnDrawColumnCell event handler."

procedure TMainForm.DBGrid1DrawColumnCell(Sender: TObject;
    const Rect: TRect; DataCol: Integer; Column: TColumn;
    State: TGridDrawState);
begin
    if DataSource1.DataSet.Fields[5].IsNull then
        DbGrid1.Canvas.Brush.Color := clRed;
    DBGrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State);
end;

If you want you can also just mark that cell as red.
0
 
da-vinciCommented:
OnDrawDataCell is not recommended anymore, this is what Borland says: "OnDrawDataCell is obsolete and included for backward compatibility. Instead, write an OnDrawColumnCell event handler."

procedure TMainForm.DBGrid1DrawColumnCell(Sender: TObject;
    const Rect: TRect; DataCol: Integer; Column: TColumn;
    State: TGridDrawState);
begin
    if DataSource1.DataSet.Fields[5].IsNull then
        DbGrid1.Canvas.Brush.Color := clRed;
    DBGrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State);
end;

If you want you can also just mark that cell as red.
0
 
SCHUCla74Author Commented:
It works !!!
Thank you all.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now