Board index » delphi » Coloring DBGrid cells

Coloring DBGrid cells

I have a table tblTempYear which includes the following fields:
Day: integer;
Color: string[2];
The values in Day are 1,2,3,... 28 to 31, depending on the month
The values in color are 'W' for white, 'P' for purple, etc.
I want to display the table values in a DBGrid, DBGrid1, so that the Day
cell is colored
according to the Color value. The following code, whcih seems to me as if it
ought to work,  does not do the job, though it compiles ok. Any ideas?

procedure TfrmMain2.DBGrid1DrawColumnCell(Sender: TObject;
  const Rect: TRect; DataCol: Integer; Column: TColumn;
  State: TGridDrawState);
begin
 if (Column.FieldName ='Day') then
    case tblTempYear.FindField('Color').AsString[1] of
    'R':
      begin
        dbGrid1.Canvas.Brush.Color := clRed;
        dbGrid1.Canvas.Font.Color := clWhite;
      end;
    'P' :
      begin
        dbGrid1.Canvas.Brush.Color := clPurple;
        dbGrid1.Canvas.Font.Color := clWhite;
      end;
    'G' :
      begin
        dbGrid1.Canvas.Brush.Color := clGreen;
        dbGrid1.Canvas.Font.Color := clWhite;
      end;
    'W':
      begin
        dbGrid1.Canvas.Brush.Color := clWhite;
        dbGrid1.Canvas.Font.Color := clBlack;
      end;
    end; // case
    DBGrid1.DefaultDrawColumnCell(Rect, DataCol, Column,State);
  end;
end;

 

Re:Coloring DBGrid cells


Never mind. I figured it out. But thanks. -- Lloyd
Quote
"Lloyd Edwards" <Llo...@sc.rr.com> wrote in message

news:zx6N8.52828$lM2.1854331@twister.southeast.rr.com...
Quote
> I have a table tblTempYear which includes the following fields:
> Day: integer;
> Color: string[2];
> The values in Day are 1,2,3,... 28 to 31, depending on the month
> The values in color are 'W' for white, 'P' for purple, etc.
> I want to display the table values in a DBGrid, DBGrid1, so that the Day
> cell is colored
> according to the Color value. The following code, whcih seems to me as if
it
> ought to work,  does not do the job, though it compiles ok. Any ideas?

> procedure TfrmMain2.DBGrid1DrawColumnCell(Sender: TObject;
>   const Rect: TRect; DataCol: Integer; Column: TColumn;
>   State: TGridDrawState);
> begin
>  if (Column.FieldName ='Day') then
>     case tblTempYear.FindField('Color').AsString[1] of
>     'R':
>       begin
>         dbGrid1.Canvas.Brush.Color := clRed;
>         dbGrid1.Canvas.Font.Color := clWhite;
>       end;
>     'P' :
>       begin
>         dbGrid1.Canvas.Brush.Color := clPurple;
>         dbGrid1.Canvas.Font.Color := clWhite;
>       end;
>     'G' :
>       begin
>         dbGrid1.Canvas.Brush.Color := clGreen;
>         dbGrid1.Canvas.Font.Color := clWhite;
>       end;
>     'W':
>       begin
>         dbGrid1.Canvas.Brush.Color := clWhite;
>         dbGrid1.Canvas.Font.Color := clBlack;
>       end;
>     end; // case
>     DBGrid1.DefaultDrawColumnCell(Rect, DataCol, Column,State);
>   end;
> end;

Other Threads