Delphi DBGrid alternate row colors for all DBGrids in the project

use*_*313 2 delphi tdbgrid delphi-xe

如何使表格中的所有网格看起来都相同?我想实现必须应用于项目的所有网格的备用行颜色。是否可以不为每个网格添加相同的 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;
Run Code Online (Sandbox Code Playgroud)

也许我需要以某种方式扩展 DBGrid,但我不知道到底如何也不知道如何在谷歌上寻找解决方案

我尝试像这样破解每个表单中的 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;
Run Code Online (Sandbox Code Playgroud)

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

有任何想法吗?

谢谢

Ken*_*ite 5

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

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;
Run Code Online (Sandbox Code Playgroud)

一些注意事项:

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

    OddRow := Boolean(DataSet.Tag);
    DataSet.Tag := Ord(not OddRow);
    
    Run Code Online (Sandbox Code Playgroud)
  • 将 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;
    
    Run Code Online (Sandbox Code Playgroud)