gpt4 book ai didi

Delphi DBGrid 项目中所有 DBGrid 的备用行颜色

转载 作者:行者123 更新时间:2023-12-01 19:11:30 28 4
gpt4 key购买 nike

如何使所有网格在我的表单上看起来都相同?我想实现必须应用于项目的所有网格的备用行颜色。是否可以不为每个网格添加相同的 DrawColumnCell 事件代码?我想避免为每个网格添加相同的代码。我的项目中有大约 30 个网格,乘以 13 行代码,它只是向我的项目添加了很多代码行,使其“不友好”。我正在寻找一种仅向项目添加 13 行代码而不是 390 行代码的解决方案。

我的格式化代码如下所示(例如):

procedure TDBGrid.DBGrid1DrawColumnCell(Sender: TObject;const Rect: TRect;DataCol: Integer;Column: TColumn;State: TGridDrawState) ;
var
grid : TDBGrid;
row : integer;
begin
grid := sender as TDBGrid;
row := grid.DataSource.DataSet.RecNo;
if Odd(row) then
grid.Canvas.Brush.Color := clSilver
else
grid.Canvas.Brush.Color := clDkGray;
grid.DefaultDrawColumnCell(Rect, DataCol, Column, State) ;
end;

可能我需要以某种方式扩展 DBGrid,但我不知 Prop 体如何或如何在谷歌上寻找解决方案

我尝试像这样破解每个表单中的 DBGRid:

type
TDBGrid = class(DBGrids.TDBGrid)
protected
procedure DrawColumnCell(const Rect: TRect; DataCol: Integer;Column: TColumn; State: TGridDrawState); override;
end;
...
procedure TDBGrid.DrawColumnCell(const Rect: TRect; DataCol: Integer;Column: TColumn; State: TGridDrawState) ;
var
grid : TDBGrid;
row : integer;
begin
row := 2;//grid.DataSource.DataSet.RecNo;
if Odd(row) then
Canvas.Brush.Color := clSilver
else
Canvas.Brush.Color := clDkGray;
DefaultDrawColumnCell(Rect, DataCol, Column, State) ;
end;

我可以做到这一点,但我无法访问发件人,因此我可以访问数据集并知道哪些记录要着色,哪些记录不着色(奇数和偶数)。无论如何,这都是一个糟糕的方法,因为我必须在每种表单上执行此操作,所以这并不是真正的解决方案

有什么想法吗?

谢谢

最佳答案

如果您将类似的内容放入数据模块中,并将其分配给每个 DBGrid 的 OnDrawColumnCell,它似乎可以工作(请参阅下面的注释):

procedure TDataModule1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
const
RowColors: array[Boolean] of TColor = (clSilver, clDkGray);
var
OddRow: Boolean;
begin
// Safety check, although it really isn't needed; no other control accepts
// this event handler definition, AFAIK, so the only way to call it with the
// wrong Sender type would be to do so in your own code manually. In my own
// code, I'd simply leave out the check and let the exception happen; if I
// was stupid enough to do so, I'd want my hand slapped rudely.
if (Sender is TDBGrid) then
begin
OddRow := Odd(TDBGrid(Sender).DataSource.DataSet.RecNo);
TDBGrid(Sender).Canvas.Brush.Color := RowColors[OddRow];
TDBGrid(Sender).DefaultDrawColumnCell(Rect, DataCol, Column, State);
end;
end;

一些注意事项:

  • 首先,您应该首先避免使用 TDataSet.RecNo,因为 BDE 后的数据集通常没有可用的值。访问它(特别是在大型数据集或基于查询的数据集上)会对您的应用程序造成重大性能影响。当然,不使用就意味着不能使用这个解决方案。更好的解决方案是使用数据集的 BeforeScroll 或 AfterScroll 事件的处理程序来切换此代码可用的 bool 值,并使用它来代替 Odd(RecNo) 的测试,或者如果数据集仅用于在 DBGrid 中显示,请在 AfterScroll 事件中使用 TDataSet.Tag 来跟踪行的奇数/偶数状态,使用

    OddRow := Boolean(DataSet.Tag);
    DataSet.Tag := Ord(not OddRow);
  • 将 DBGrids 添加到数据模块的 use 子句中,并在 published 部分中手动声明上述事件,以便所有使用该数据模块的单元都可以使用该事件。然后,您可以照常在“对象检查器事件”选项卡中从这些单元中分配它。

  • 这不能正确处理TGridDrawState(您的初始代码也不能正确处理)。您需要自己添加处理,因为这不是您在这里要求的。

  • 根据奇数行和偶数行所需的颜色,您可能需要反转 RowColors 中颜色的顺序。

  • 我更喜欢重复的类型转换,这样代码的作用就很清楚了。如果这让您烦恼,您可以简单地声明一个局部变量:

    var
    OddRow: Boolean;
    Grid: TDBGrid;
    begin
    if (Sender is TDBGrid) then
    begin
    Grid := TDBGrid(Sender);
    OddRow := Odd(Grid.DataSource.DataSet.RecNo);
    ...
    end;
    end;

关于Delphi DBGrid 项目中所有 DBGrid 的备用行颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22083444/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com