Mirror

How to change the appearance of the focus rectangle in a TDBGrid (Views: 711)


Problem/Question/Abstract:

I would like to be able to change the colour of the focus rectangle for certain cells and also to prevent it from being drawn for certain cells even when they have focus - is this possible? I have a graphic displayed in a column and I don't want it to be obscured by the blue focus rectangle - a transparent focus rectangle would be ok.

Answer:

procedure TForm1.DBGrid1DrawDataCell(Sender: TObject; const Rect: TRect;
  Field: TField; State: TGridDrawState);
var
  x: string;
begin
  if Field.value = Null then
    x := ''
  else
    x := FloatToStr(field.value);
  with Sender as TDbGrid do
  begin
    if gdFocused in State then
    begin
      Canvas.Brush.Color := clYellow;
      Canvas.Font.color := ClBlack;
    end;
    Canvas.FillRect(Rect);
    Canvas.TextOut(Rect.Left + Canvas.Font.Size, Rect.Top + 2, x);
    if gdFocused in State then
      Canvas.DrawFocusRect(Rect);
  end;
end;

<< Back to main page