gpt4 book ai didi

delphi - 如何为DBGrid特殊单元格着色?

转载 作者:行者123 更新时间:2023-12-03 15:18:22 25 4
gpt4 key购买 nike

我有一列只有"is"和“否”值。我希望如果列值为"is",则只有单元格背景颜色为红色否则“否”则背景颜色为黄色但这段代码为整行着色:

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。

enter image description here

最佳答案

您需要添加一个条件来限制画笔颜色仅更改到您选择的列。在代码中可能是:

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特殊单元格着色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27026043/

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