关于DBGrid的颜色控制的问题(100分)

  • 主题发起人 主题发起人 cdzerg
  • 开始时间 开始时间
C

cdzerg

Unregistered / Unconfirmed
GUEST, unregistred user!
假设我在DBgrid中显示了如下数据:
0 2
1 -1
21 -3
0 4
0 -2
我希望某单元格的数值是0时,则该单元格的字体用红色显示;
某单元格的数值时负数时,则该单元格所在的行的背景色用黄色显示。
 
不知道,你学的挺厉害,学了多久了。
 
从以前的DFW记录中摘抄:供参考
procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
var
Code: Integer;
Bmp: TBitmap;
begin
// erase existing output
DBGrid1.Canvas.FillRect (Rect);

if Column.Field = Table1Graphic then
begin
// draw the image
Bmp := TBitmap.Create;
try
Bmp.Assign (Table1Graphic);
DBGrid1.Canvas.StretchDraw (Rect, Bmp);
finally
Bmp.Free;
end;
end
else
begin
// choose the font color
if (Column.Field = Table1Lengthcm) and
(Table1Lengthcm.AsInteger > 100) then
DBGrid1.Canvas.Font.Color := clRed
else if gdSelected in State then
DBGrid1.Canvas.Font.Color := clHighlightText
else
DBGrid1.Canvas.Font.Color := Column.Font.Color;
// draw the standard text
DBGrid1.Canvas.TextRect (
Rect, Rect.Left, Rect.Top,
Column.Field.AsString);
end;

// optionally draw the focus rectangle
if gdFocused in State then
DBGrid1.Canvas.DrawFocusRect (Rect);
end;

delphi4从入门道精通上的例子

 
OK了,谢谢gong666!

 
后退
顶部