说实在的,我还没写过,但这段代码肯定对你有帮助
你可在 StringGrid 元件的 DrawDataCell 事件中, 依资料的条件性来改变格子或文字的颜色.如 :
OnDrawDataCell(...)
begin
with TStringGrid(Sender) do
begin
if (条件) then
Canvas.TextOut(Rect.Left + 4, Rect.Top + 2, '要显示的文字, 如表格的资料');
end;
而你会看到 StringGrid 的显示资料怎麽有重叠的情况, 那是因为原本StringGrid
要显示的资料与 TextOut 所显示的资料重叠, 解决方法 :
在 Query 元件所加入的栏位(在元件上按右键, 会有 Add Fields...的选单), 在
不要显示资料的栏位的 OnGetText 事件中有一参数设定为 False;
procedure TForm1.Query1Detail1GetText(Sender: TField; var Text: string;
DisplayText: Boolean);
begin
// 决定在 StringGrid 得知表格资料时, 要不要显示所得到的资料, False -> 不显示
// 就可避免与 TextOut 的文字重叠了
DisplayText : = False;
end;
end;
如果用 Delphi 3 处理很简单.
例如,对表中某字段当其数值小于0时为红字,其他为黑字.
在 StringGrid.OnDrawColumnCell(...) 中:
begin
if TableField.AsInteger < 0 then
StringGrid.Canvas.Font.Color := clRed
else
StringGrid.Canvas.Font.Color := clBlack;
StringGrid.DefaultDrawColumnCell(...);
end;
这样,对 Field 指定的格式仍旧生效,不必重写.
end;