Recent

Author Topic: Colorir algumas linhas em StringGrid  (Read 5100 times)

fernandocg

  • New member
  • *
  • Posts: 7
Colorir algumas linhas em StringGrid
« on: March 19, 2015, 11:31:24 pm »
Estou tentando usar uma condição para colorir somente algumas linhas em um StringGrid. Abaixo o código:

procedure TFrmPrincipal.StrgMovDrawCell(Sender: TObject; aCol, aRow: Integer;
  aRect: TRect; aState: TGridDrawState);
var
  i : integer;
begin
  i := 1;
  with StrgMov do
    begin
      if Time - StrToTime(StrgMov.Cells[3, i]) > StrToTime('00:15:00') then //verifica se o tempo é maior que 15 minutos.
          if StrgMov.Cells[4, i] = 'E' then //verifica o tipo de cliente
            begin
              Canvas.Brush.Color := clRed;
              Canvas.FillRect(aRect);
              Inc(i);
            end;
    end;
  StrgMov.canvas.textrect(arect, 0, 0, StrgMov.cells[acol,arow]);
end;

Ele fica todo em vermelho e desmontado. a imagem está anexada.

Obrigado.

wp

  • Hero Member
  • *****
  • Posts: 13644
Re: Colorir algumas linhas em StringGrid
« Reply #1 on: March 20, 2015, 12:04:42 am »
Do not use DrawCell. Use the OnPrepareCanvas event instead:

(Google-translated: Não use DrawCell. Use o evento OnPrepareCanvas em vez disso)

Code: [Select]
procedure TForm1.StringGrid1PrepareCanvas(sender: TObject; aCol, aRow: Integer; aState: TGridDrawState);
begin
  if ARow = 5 then
    StringGrid1.Canvas.Brush.Color := clRed;
end;

Never

  • Sr. Member
  • ****
  • Posts: 409
  • OS:Win7 64bit / Lazarus 1.4
Re: Colorir algumas linhas em StringGrid
« Reply #2 on: March 20, 2015, 12:30:18 am »
well you are missing a [begin] and [end] here
Quote
      if Time - StrToTime(StrgMov.Cells[3, i]) > StrToTime('00:15:00') then //verifica se o tempo é maior que 15 minutos.
Begin<------------------
          if StrgMov.Cells[4, i] = 'E' then //verifica o tipo de cliente
       
            begin
              Canvas.Brush.Color := clRed;
              Canvas.FillRect(aRect);
              Inc(i);
            end;
End<--------------

change StrgMov.Cells[4, i] to StrgMov.Cells[4, aRow]
and it will work
remove i var
« Last Edit: March 20, 2015, 12:33:47 am by Never »
Νέπε Λάζαρε λάγγεψων οξωκά ο φίλοσ'ς αραεύσε

fernandocg

  • New member
  • *
  • Posts: 7
Re: Colorir algumas linhas em StringGrid
« Reply #3 on: March 20, 2015, 12:35:59 am »
Thanks WD,

Now i put:

procedure TFrmPrincipal.StrgMovPrepareCanvas(sender: TObject; aCol,
  aRow: Integer; aState: TGridDrawState);
var
  i : integer;
begin
  i := 1;
  with StrgMov do
    begin
      if not(gdFixed in aState) then //linha de título
        begin
          if Time - StrToTime(StrgMov.Cells[3, i]) > StrToTime('00:15:00') then //verifica se o tempo é maior que 15 minutos.
            if StrgMov.Cells[4, i] = 'E' then //verifica o tipo de cliente
              begin
                Canvas.Brush.Color := clRed;
                Inc(i);
              end
            else
              Canvas.Brush.Color := clDefault;
        end;
    end;
end;

But all rows are in red.

fernandocg

  • New member
  • *
  • Posts: 7
Re: Colorir algumas linhas em StringGrid
« Reply #4 on: March 20, 2015, 12:39:59 am »
Tanks guys!

Now its OK!

Take care...

wp

  • Hero Member
  • *****
  • Posts: 13644
Re: Colorir algumas linhas em StringGrid
« Reply #5 on: March 20, 2015, 12:47:07 am »
There is a very good wiki page on grids: http://wiki.lazarus.freepascal.org/Grids_Reference_Page

 

TinyPortal © 2005-2018