Link to home
Start Free TrialLog in
Avatar of Scay7
Scay7Flag for South Africa

asked on

DBGRID on cell draw

Ello all, cant for the life of me figure out why it wont stick...
I have a dbgrid.ondrawcell procedure that draws the colours fine but it wont keep the bitmaps in the cell only when you click or highlight the cell does the bitmap appear but if you move the the next record the bitmap disappears....

Here is the procedure...

Procedure dw(rect: trect; imgl : integer);
var
bitmap : TBitmap;
begin
  bitmap := TBitmap.Create;
  try
    form1.ImgList.GetBitmap(imgl,bitmap);
    form1.dbgrid.Canvas.FillRect(rect);
    form1.DBGrid.Canvas.Draw(Rect.left,rect.Top+1,bitmap);
  finally
  bitmap.Free;
  end;
end;

procedure TForm1.DBGridDrawColumnCell(Sender: TObject; const Rect: TRect;
  DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin

if Column.Field.Dataset.FieldbyName('dnfee').AsString = '1' then
  if column.FieldName = 'ico' then dw(rect,1);
if Column.Field.Dataset.FieldbyName('oasts').AsString = '' then
  if column.FieldName = 'ico' then dw(rect,0);

If Column.Field.Dataset.FieldbyName('dbcs').AsString = '4' then 
dbgrid.canvas.brush.color := clyellow;
If Column.Field.Dataset.FieldbyName('dbcs').AsString = '6' then 
dbgrid.canvas.brush.color := clmoneygreen;
If Column.Field.Dataset.FieldbyName('dbcs').AsString = '7' then 
dbgrid.canvas.brush.color := clskyblue;
If Column.Field.Dataset.FieldbyName('dbcs').AsString = '12' then 
dbgrid.canvas.brush.color := cllime;
If Column.Field.Dataset.FieldbyName('dbcs').AsString = '14' then 
dbgrid.canvas.brush.color := clfuchsia;

If Column.Field.Dataset.FieldbyName('dbcs').AsString = '1' then
begin
dbgrid.canvas.Font.Color := clcream;
dbgrid.canvas.brush.color := clred;
end;
If Column.Field.Dataset.FieldbyName('dbcs').AsString = '2' then
begin
dbgrid.canvas.Font.Color := clcream;
dbgrid.canvas.brush.color := clgreen;
end;
If Column.Field.Dataset.FieldbyName('dbcs').AsString = '3' then
begin
dbgrid.canvas.Font.Color := clcream;
dbgrid.canvas.brush.color := clblue;
end;
If Column.Field.Dataset.FieldbyName('dbcs').AsString = '5' then
begin
dbgrid.canvas.Font.Color := clcream;
dbgrid.canvas.brush.color := clmaroon;
end;
If Column.Field.Dataset.FieldbyName('dbcs').AsString = '8' then
begin
dbgrid.canvas.Font.Color := clcream;
dbgrid.canvas.brush.color := clolive;
end;
If Column.Field.Dataset.FieldbyName('dbcs').AsString = '9' then
begin
dbgrid.canvas.Font.Color := clcream;
dbgrid.canvas.brush.color := clnavy;
end;
If Column.Field.Dataset.FieldbyName('dbcs').AsString = '10' then
begin
dbgrid.canvas.Font.Color := clcream;
dbgrid.canvas.brush.color := clpurple;
end;
If Column.Field.Dataset.FieldbyName('dbcs').AsString = '11' then
begin
dbgrid.canvas.Font.Color := clcream;
dbgrid.canvas.brush.color := clgray;
end;
If Column.Field.Dataset.FieldbyName('dbcs').AsString = '13' then
begin
dbgrid.canvas.Font.Color := clcream;
dbgrid.canvas.brush.color := clteal;
end;

dbgrid.DefaultDrawColumnCell(rect,DataCol,Column,State);
end;

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Emmanuel PASQUIER
Emmanuel PASQUIER
Flag of France image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
you 'dbcs' fields looks like not a string field but integer field. If you accessed it as such, you could do some optimizations (and your code would be more readable)

if Column.Field.Dataset.FieldbyName('dbcs').AsInteger In [1..3,5,8..11,13] 
 Then dbgrid.canvas.Font.Color := clcream;

Open in new window


you might also want to check the TGridDrawState before changing always the colors

ex.
 if gdSelected In State Then dbgrid.canvas.brush.color := clNavy;
Avatar of Scay7

ASKER

Ag nooit man when you blind you are blind, didnt even notice that i was calling it twice never mind the placing, thanks alot man, programs looking good now cheers
Avatar of Scay7

ASKER

naa its a string 'dbcs' and i need to keep it that way for future purposes... besides to many variations on that part so the way its set out now is the best i could come up with (plus future operations that might come into play later on)

But thanks again for the assist...
also, you can draw directly from the ImageList without having to get the bitmap first :

if column.FieldName = 'ico' then
 begin
  if Column.Field.Dataset.FieldbyName('dnfee').AsString = '1' 
   then ImgList.Draw(DBGrid.Canvas,Rect.Left,Rect.Top+1, 1 );
  if Column.Field.Dataset.FieldbyName('oasts').AsString = '' 
   then ImgList.Draw(DBGrid.Canvas,Rect.Left,Rect.Top+1, 0 );
 end;

Open in new window