Topic: color rows alternately on a tablegrid when there are many records

I use the procedure below to alternately color the lines of a tablegrid. This routine, I found here in this forum.

However, navigation slows down on a 22 row tablegrid because it is linked to a table with 4000 records. I noticed that using the tablegrid dblimit of 22 lines, the browsing speed is good. But,
As you scroll down the tablegrid, the colors fade. Can anyone help?

procedure ColorRowGrid (Grid:TdbStringGridEx;ColorEven,ColorOdd:TColor;ajusta:boolean);
//**** alterna cores de linhas numa tablegrid
var
   iRow ,c: integer;
   q, iCol: integer;
begin
     c := Grid.RowCount - 1;
     q := Grid.Columns.Count-1;
     for iRow := 0 to c do
         for iCol := 0 to q do
         begin
             if iRow mod 2 = 0 then Grid.Cell[iCol,iRow].Color := ColorEven
             else
             Grid.Cell[iCol,iRow].Color := ColorOdd;
         end;
         if ajusta then Grid.BestFitColumns(bfBoth); //ajusta conteúdo no tablegrid
end;

Roberto Alencar

2 (edited by domebil 2019-11-11 06:35:34)

Re: color rows alternately on a tablegrid when there are many records

I use this and I have no problems

procedure TableGrid_OnChange (Sender: TdbStringGridEx);
var
   i,k,r,c: integer;
begin
     r := Sender.RowCount - 1;
     c := Sender.Columns.Count - 1;
     for i := 0 to r do
          begin
           if i mod 2 = 0 then
              begin
                for k := 0 to c do
                  begin
                    Sender.Cell[k,i].Color := $00FBFBFB;
                  end;
              end;
          end;
end;
Domebil

Re: color rows alternately on a tablegrid when there are many records

Try to add BeginUpdate and EndUpdate to increase speed of coloring

var
   iRow ,c: integer;
   q, iCol: integer;
begin
     Grid.BeginUpdate;
     c := Grid.RowCount - 1;
     q := Grid.Columns.Count-1;
     for iRow := 0 to c do
         for iCol := 0 to q do
         begin
             if iRow mod 2 = 0 then Grid.Cell[iCol,iRow].Color := ColorEven
             else
             Grid.Cell[iCol,iRow].Color := ColorOdd;
         end;
         if ajusta then Grid.BestFitColumns(bfBoth); //ajusta conteúdo no tablegrid
         Grid.EndUpdate;
end;
Dmitry.

Re: color rows alternately on a tablegrid when there are many records

thank you

Roberto Alencar