Page 1 of 1

dbgrid cell color help

PostPosted: May 29th, 2009, 5:43 am
by danielHP
Hi, I'm new to delphi so I'm sorry if my question seem amateurish.
I want to create an application where some dbgrid cells in a column have a different background color. The color can be changed by changing RGB value. I give a picture to illustrate my point.
sample.jpg
sample.jpg (22.41 KiB) Viewed 1264 times

Can it be done?
If you can give a sample code,it will be much appreciated.

Thankyou.

Re: dbgrid cell color help

PostPosted: May 29th, 2009, 12:45 pm
by Kambiz
Suppose you have three fields named R, G, and B.
Define a CalculatedField for your table to calculate the RGB value.

Code: Select all
procedure TForm1.Table1CalcFields(DataSet: TDataSet);
begin
  Table1RGB.Value := RGB(Table1R.Value, Table1G.Value, Table1B.Value);
end;

Then use OnDrawDataCell event of the DBGrid do draw the RGB cell.

Code: Select all
procedure TForm1.DBGrid1DrawDataCell(Sender: TObject; const Rect: TRect;
  Field: TField; State: TGridDrawState);
begin
  if Field = Table1RGB then
  begin
    DBGrid1.Canvas.Brush.Color := Table1RGB.Value;
    DBGrid1.Canvas.FillRect(Rect);
  end;
end;

Re: dbgrid cell color help

PostPosted: May 30th, 2009, 1:56 am
by danielHP
thankyou very much!! I will test it now.