Paint Dbgrid lines by selecting them

Asked

Viewed 4,172 times

6

I’m painting the lines of a , in the , but my condition to paint the selected line is not working properly.

When selecting the row (set the focus) only the first column is being painted. Example:

Exemplo da DBGrid com a linha selecionada

The estate Options, dgRowSelect is set as True.

My code to paint the lines and paint the line when it is selected, as found in the web example:

procedure TFrmCadTerminal.dbgTerminaisDrawColumnCell(Sender: TObject;
  const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
  inherited;
  if cdsTerminal.IsEmpty then exit;

  if cdsTerminalAcess_TerE.AsString <> 'S' then
  begin
    if gdFocused in State then
    begin
      dbgTerminais.Canvas.Brush.Color := clRed;
      dbgTerminais.Canvas.Font.Color := clWhite;
      dbgTerminais.Canvas.FillRect(Rect);
      dbgTerminais.DefaultDrawDataCell(Rect, Column.Field, State);
    end
    else
    begin
      dbgTerminais.Canvas.Brush.Color := clMaroon;
      dbgTerminais.Canvas.Font.Color := clWhite;
      dbgTerminais.Canvas.FillRect(Rect);
      dbgTerminais.DefaultDrawDataCell(Rect, Column.Field, State);
    end;
  end;
end;

I would like the entire line to be painted the same color when selecting it. How to solve this?

1 answer

5


Do it this way:

procedure TFrmCadLicenca.dbgTerminaisDrawColumnCell(Sender: TObject;
  const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
  inherited;
  if cdsTerminal.IsEmpty then exit;

  if cdsTerminalAcess_TerE.AsString <> 'S' then
  begin
    if (gdSelected in State) and dbgTerminais.Focused then
    begin
      dbgTerminais.Canvas.Brush.Color := clRed;
      dbgTerminais.Canvas.Font.Color := clWhite;
      dbgTerminais.DefaultDrawDataCell(Rect, Column.Field, State);
    end
    else
    begin
      dbgTerminais.Canvas.Brush.Color := clMaroon;
      dbgTerminais.Canvas.Font.Color := clWhite;
      dbgTerminais.DefaultDrawDataCell(Rect, Column.Field, State);
    end;
  end;
end;

I advise you to test with the and additional, the dbgTerminais.Focused.

  • Thanks for the tip, saved me the day.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.