简体   繁体   English

DBGrid 上的行组的替代颜色

[英]Alternate color of groups of rows on a DBGrid

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).我的第一次尝试是向 ClientDataset 添加一个 GroupNumber(使用 SQL 服务器的 DENSE_RANK() function)。

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:现在我可以使用以下代码在网格上交替 colors :

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.谢谢你。

There is no 'magic' function for what you want: use alternate color based on some field value (aka group) when the list is sorted on that field value/group.对于您想要的,没有“魔法” function:当列表按该字段值/组排序时,使用基于某个字段值(又名组)的替代颜色。

But you could make your own list of groups (based on the dataset content) and then use the even/odd specification of the index of the group in that list to get the color.但是您可以制作自己的组列表(基于数据集内容),然后使用该列表中组索引的偶数/奇数规范来获取颜色。

When you delete rows, check to see if its the last row for that group and then delete the group from the list.当您删除行时,请检查它是否是该组的最后一行,然后从列表中删除该组。 Other way around, when a record is added with a new group, add the new group to the group list.反之,当记录与新组一起添加时,将新组添加到组列表中。

Small note: the order of the groups in the list must be the same as the order in which the groups appear in the dataset!小提示:列表中组的顺序必须与组在数据集中出现的顺序一致!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM