Lazarus

Programming => Databases => Topic started by: piper62 on November 14, 2007, 07:12:43 am

Title: DBGrid coloring problem
Post by: piper62 on November 14, 2007, 07:12:43 am
Hi,
I have a problem in coloring of a DBGrid.
I can set with the following code the font color but the background of the cells isn't changed.
v_color is set on Form.Show to an initial state of clBlack.

procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
  DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
   v_buffer_new:=ZQuery1.FieldByName('md_row_date').AsString;
   if ZQuery1.Active then
   begin
      if v_buffer_new=v_buffer_old then
      begin
         DBGrid1.Canvas.Font.Color:=v_color;
         DBGrid1.Canvas.Brush.Color:=clSilver;
         DBGrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State);
      end
      else
      begin
         if v_color=clBlack then v_color:=clBlue
         else v_color:=clBlack;
         DBGrid1.Canvas.Font.Color:=v_color;
         DBGrid1.Canvas.Brush.Color:=clBlack;
         DBGrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State);
         v_buffer_old:=v_buffer_new;
      end;
   end;
end;

The complete thing is running under Lazarus 0.9.22 with FP 2.0.4 on Debian Linux with GTK1.

Has somebody an idea why only the font is changed?

Help appreciated.

Regards,
Tibor
Title: RE: DBGrid coloring problem
Post by: Leledumbo on November 14, 2007, 09:40:48 am
Why don't you change color property of dbgrid in the object inspector? I have no problem changing the color of the background from there. Well, I don't know if it's a bug of Lazarus 0.9.22 or not, but I guess it's better to try 0.9.23 with fpc 2.2.0. At least, upgrade the fpc, 2.2.0 is a stable release.
Title: RE: DBGrid coloring problem
Post by: jesusr on November 14, 2007, 06:49:16 pm
The problem is that DefaultDrawColumnCell only draws text (layout, color etc), if you use OnDrawColumnCell you should also draw cell background.

From your code it looks like you want to do custom drawing but only to change font and cell color, There are 2 ways to fix this, one is using DBGrid1.canvas.FillRect(aRect); to clear the draw background using the canvas brush, or (better IMO) to use OnPrepareCanvas, there you only change canvas attributes and don't need to actually draw anything, you need to remove OnDrawColumnCell though.
Title: RE: DBGrid coloring problem
Post by: piper62 on November 15, 2007, 12:15:14 am
thanx for the hints! Now it works with DBGrid1.canvas.FillRect(Rect);
with the code the bevaviour of the DBGrid is now as I wanted it to have.
It's a medical application where this modul shows documentation lines regarding to the patient. and there can be MANY of them over the time. so it becomes difficult to have a good overview about the documentation if there's no differentiation between lines which belong to the same date.
So, the Grid no changes the color of the lines where the date changes so that the physician sees on one view the documentation lines which belong together.

Thanx again!
Tibor
TinyPortal © 2005-2018