I want to alternate the colours of my grid by groups. My first try is doing this adding a GroupNumber to the ClientDataset (using the DENSE_RANK() function of SQL Server).
select dense_rank() over (order by Viatge) as GroupNumber,
Transports_v.*
from Transports_v
where IdTransportista = :Id
order by 1
Now I can alternate colors on the grid using this code:
procedure TFFacturesTransportista.AEDBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
if Odd(QAlbaransPEndentsGroupNumber.Value) then AEDBGrid1.Canvas.Brush.Color := clInfoBk;
AEDBGrid1.DefaultDrawColumnCell(Rect,DataCol,Column,state);
end;
It works well, but if I manually delete a row then I can have two consecutive even or odd groups, and they will be drawn with the same colour.
Is there a better way to check if the current record starts a new group ?.
Thank you.