dbgrid cell color help

Please discuss general Delphi programming topics here.

dbgrid cell color help

Postby danielHP » May 29th, 2009, 5:43 am

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 1599 times

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

Thankyou.
danielHP
Member
Member
 
Posts: 3
Joined: May 29th, 2009, 5:18 am

Re: dbgrid cell color help

Postby Kambiz » May 29th, 2009, 12:45 pm

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;
Kambiz
User avatar
Kambiz
Administrator
Administrator
 
Posts: 2429
Joined: March 7th, 2003, 7:10 pm

Re: dbgrid cell color help

Postby danielHP » May 30th, 2009, 1:56 am

thankyou very much!! I will test it now.
danielHP
Member
Member
 
Posts: 3
Joined: May 29th, 2009, 5:18 am


Return to Delphi Programming

Who is online

Users browsing this forum: No registered users and 2 guests

cron