Delphi – How to color to DBGRID?

I have a column with only “Yes” and “No” values.
I think if the column value is “Yes”, only the cell background color will be red
Otherwise “No” and the background color is yellow
But this code color the entire line:

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

Edit

Thank you for your reply.
My real code looks like that. The “netice” column only has “L,D,W”.

< /p>

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.

You need to add conditions to limit Only change the brush color to the column of your choice. In the code it may 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< br /> // 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 am before Column.FieldName Prefer this, because Column.FieldName is not yet guaranteed to link data There are such fields in the collection. Therefore, it is safer to access the site directly in this way.

I have a column with only “yes” and “no” values.
I think if the column value is “Yes”, only the cell background color is red
Otherwise “No” and the background color is yellow
But this code color the entire row:

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

Edit

Thank you for your reply.
My real code looks like that . “Netice” column only has “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.

You need to add a condition to restrict only the brush color to the column you choose. In the code it might be:

< pre>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 prefer this before Column.FieldName, because Column.FieldName does not guarantee that there are such fields in the linked dataset. Therefore, it is safer to directly access the site in this way.

Leave a Comment

Your email address will not be published.