5

I have a column which have only "yes" and "no" values. I want if column value is "yes" then only that cell background color is red else "no" then background color is yellow but this code colors whole row :

if ADOTable1.FieldByName('Clubs').AsString = 'yes' then
begin
  DBGrid1.Canvas.Brush.Color := clRed;
  DBGrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State);
end;

EDIT

Thanks for your replies. My real code look like that. The "netice" column only have "L, D, W,".

if Column.FieldName = 'netice' then
 begin
 if ADOTable1.FieldByName('netice').AsString = 'L' then
 DBGrid1.Canvas.Brush.Color := clgreen ;
 if ADOTable1.FieldByName('netice').AsString = 'D' then
 DBGrid1.Canvas.Brush.Color := clRed ;
 if ADOTable1.FieldByName('netice').AsString = 'W' then
 DBGrid1.Canvas.Brush.Color := clYellow ;
 end;
 DBGrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State);
 end;

but I need L--green, D--red, W--yellow I am using Delphi 2010.

enter image description here

bummi
  • 26,435
  • 13
  • 58
  • 97
Mamed Aliyev
  • 111
  • 1
  • 2
  • 10
  • Thanks for your reply. This code colors the row . but I need to color only cells in which value "yes" or "no" – Mamed Aliyev Nov 19 '14 at 20:38
  • Are you sure Adotable1 is the Dataset bound to the grid, or might there be anonther one, you should better use: `var ADS:TDataset; begin Ads := TDBGrid(Sender).DataSource.DataSet; if Column.FieldName = 'netice' then begin if Ads.FieldByName('netice').AsString = 'L' then DBGrid1.Canvas.Brush.Color := clgreen ; .........` – bummi Nov 20 '14 at 17:43
  • @bummi, `Column.Field` gives you the exact linked dataset field. Hence I've used it in my code below. But here I'm not feeling much safe from that `FieldName` comparison (the OP used first capital in the first case, not in the latter). Maybe something [`like this`](http://pastebin.com/SufF1kz8) might work. – TLama Nov 22 '14 at 13:08

2 Answers2

11

You need to add a condition to restrict changing of the brush color only to the column of your choice. In code it could be:

procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
  DataCol: Integer; Column: TColumn; State: TGridDrawState);
var
  Field: TField;
begin
  // store the currently rendered cell's column assigned field reference
  // (if any) to the local variable (there's quite expensive getter)
  Field := Column.Field;
  // if the rendered cell's column has assigned a field and this field's
  // name is 'Clubs' (compared without case sensitivity), then, and only
  // then change the brush color...
  if Assigned(Field) and SameText(Field.FieldName, 'Clubs') then
  begin
    if Field.AsString = 'yes' then
      DBGrid1.Canvas.Brush.Color := clRed
    else
      DBGrid1.Canvas.Brush.Color := clYellow;
  end;
  DBGrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State);
end;

I'd prefer this before Column.FieldName because Column.FieldName does not yet guarantee that there is such field in the linked dataset. Accessing the field directly is therefore safer in this manner.

TLama
  • 71,521
  • 15
  • 192
  • 348
  • 2
    P.S. try to choose a different DB column type. String is not an optimal choice for storing boolean values. – TLama Nov 19 '14 at 21:20
3

You can do it this way:

if Column.FieldName = 'Clubs' then
begin
  if ADOTable1.FieldByName('Clubs').AsString = 'yes' then
    DBGrid1.Canvas.Brush.Color := clRed
  else
    DBGrid1.Canvas.Brush.Color := clYellow;
end;
DBGrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State);
TLama
  • 71,521
  • 15
  • 192
  • 348
Amir Rahimi Farahani
  • 1,530
  • 1
  • 11
  • 14