如何通过Delphi编程为特定DBGrid单元格实现个性化着色方案?

2026-04-10 20:052阅读0评论SEO问题
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计435个文字,预计阅读时间需要2分钟。

如何通过Delphi编程为特定DBGrid单元格实现个性化着色方案?

我有这样一个只有是和否值的列表。我想如果列表值为是,则只单元格背景颜色为红色;否则,否背景颜色为黄色。但是此代代码为整行着色: if ADOTable1.FieldByName('Clubs').AsStr

我有一个只有“是”和“否”值的列.
我想如果列值为“是”,则只有该单元格背景颜色为红色
否则“否”然后背景颜色为黄色
但是此代码为整行着色:

if ADOTable1.FieldByName('Clubs').AsString = 'yes' then begin DBGrid1.Canvas.Brush.Color := clRed; DBGrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State); end;

编辑

谢谢你的回复.
我的真实代码看起来像那样. “netice”列只有“L,D,W”.

if Column.FieldName = 'netice' then begin if ADOTable1.FieldByName('netice').AsString = 'L' then DBGrid1.Canvas.Brush.Color := clgreen ; if ADOTable1.FieldByName('netice').AsString = 'D' then DBGrid1.Canvas.Brush.Color := clRed ; if ADOTable1.FieldByName('netice').AsString = 'W' then DBGrid1.Canvas.Brush.Color := clYellow ; end; DBGrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State); end;

但我需要L – 绿色,D – 红色,W – 黄色
我正在使用Delphi 2010.

您需要添加条件以限制仅将刷子颜色更改为您选择的列.在代码中它可能是:

procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState); var Field: TField; begin // store the currently rendered cell's column assigned field reference // (if any) to the local variable (there's quite expensive getter) Field := Column.Field; // if the rendered cell's column has assigned a field and this field's // name is 'Clubs' (compared without case sensitivity), then, and only // then change the brush color... if Assigned(Field) and SameText(Field.FieldName, 'Clubs') then begin if Field.AsString = 'yes' then DBGrid1.Canvas.Brush.Color := clRed else DBGrid1.Canvas.Brush.Color := clYellow; end; DBGrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State); end;

我在Column.FieldName之前更喜欢这个,因为Column.FieldName还不能保证链接数据集中有这样的字段.因此,以这种方式直接访问现场更安全.

如何通过Delphi编程为特定DBGrid单元格实现个性化着色方案?

本文共计435个文字,预计阅读时间需要2分钟。

如何通过Delphi编程为特定DBGrid单元格实现个性化着色方案?

我有这样一个只有是和否值的列表。我想如果列表值为是,则只单元格背景颜色为红色;否则,否背景颜色为黄色。但是此代代码为整行着色: if ADOTable1.FieldByName('Clubs').AsStr

我有一个只有“是”和“否”值的列.
我想如果列值为“是”,则只有该单元格背景颜色为红色
否则“否”然后背景颜色为黄色
但是此代码为整行着色:

if ADOTable1.FieldByName('Clubs').AsString = 'yes' then begin DBGrid1.Canvas.Brush.Color := clRed; DBGrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State); end;

编辑

谢谢你的回复.
我的真实代码看起来像那样. “netice”列只有“L,D,W”.

if Column.FieldName = 'netice' then begin if ADOTable1.FieldByName('netice').AsString = 'L' then DBGrid1.Canvas.Brush.Color := clgreen ; if ADOTable1.FieldByName('netice').AsString = 'D' then DBGrid1.Canvas.Brush.Color := clRed ; if ADOTable1.FieldByName('netice').AsString = 'W' then DBGrid1.Canvas.Brush.Color := clYellow ; end; DBGrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State); end;

但我需要L – 绿色,D – 红色,W – 黄色
我正在使用Delphi 2010.

您需要添加条件以限制仅将刷子颜色更改为您选择的列.在代码中它可能是:

procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState); var Field: TField; begin // store the currently rendered cell's column assigned field reference // (if any) to the local variable (there's quite expensive getter) Field := Column.Field; // if the rendered cell's column has assigned a field and this field's // name is 'Clubs' (compared without case sensitivity), then, and only // then change the brush color... if Assigned(Field) and SameText(Field.FieldName, 'Clubs') then begin if Field.AsString = 'yes' then DBGrid1.Canvas.Brush.Color := clRed else DBGrid1.Canvas.Brush.Color := clYellow; end; DBGrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State); end;

我在Column.FieldName之前更喜欢这个,因为Column.FieldName还不能保证链接数据集中有这样的字段.因此,以这种方式直接访问现场更安全.

如何通过Delphi编程为特定DBGrid单元格实现个性化着色方案?