Jump to content


Photo

Alternating row coloring


  • Please log in to reply
3 replies to this topic

#1 zifnabbe

zifnabbe
  • Members
  • 31 posts

Posted 07 April 2006 - 08:43 PM

For alternating row color, I use the following code:

CODE
procedure TNextGridFrame.dbgAddInCellColoring(Sender: TObject; ACol,

 ARow: Integer; var CellColor, GridColor: TColor; CellState: TCellState);

begin

 if (ACol > 1) then

 begin

   if (not (csSelected in CellState))

       and (ARow mod 2 = 0) then CellColor := RGB(245, 245, 245);

 end;

end;


However, this also colors rows where no data is available. So if you've a grid which displays an empty dataset, then you get alternating line coloring for the totality of the grid. This is confusing for the user. Is there a way to detect if the cell/row contains data.

Thanks.

#2 Boki (Berg)

Boki (Berg)

    Boki (Berg)

  • Forum Admin
  • PipPipPipPipPip
  • 8,289 posts
  • Gender:Male

Posted 08 April 2006 - 12:46 AM

Hello,

Please try next code:

CODE
procedure TForm1.NextDBGrid1CellColoring(Sender: TObject; ACol,

 ARow: Integer; var CellColor, GridColor: TColor; CellState: TCellState);

begin

 if (ARow < NextDBGrid1.RowCount) then

 begin

   if (not (csSelected in CellState))

       and (ARow mod 2 = 0) then CellColor := RGB(245, 245, 245);

 end;

end;


regards
boki@bergsoft.net | LinkedIn Profile
--
BergSoft Home Page: www.bergsoft.net
Users Section: users.bergsoft.net
Articles and Tutorials: help.bergsoft.net (Developers Network)
--
BergSoft Facebook page
--
Send us applications made with our components and we will submit them on: www.bergsoft.net/apps.htm. Link to this page will be also set on home page too.

#3 zifnabbe

zifnabbe
  • Members
  • 31 posts

Posted 08 April 2006 - 01:17 AM

Great, thanks!

#4 nevel

nevel
  • Members
  • 8 posts

Posted 05 August 2011 - 03:55 PM

You may also want to check for the csEmpty state, like so:

if (not (csSelected in CellState)) and (not (csEmpty in CellState)) then begin
if ARow mod 2 = 0 then begin
CellColor := RGBToColor(255, 255, 255);
end
else begin
CellColor := RGBToColor(248, 248, 248);
end;
end;

If you don't, the alternating colors will remain visible after the number of grid items has decreased.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users