如何使dbgrid中不同的值显示不同颜色
【打印文章】
在DBGrid的OnDrawDataCell事件下写:
procedure TForm1.DBGrid1DrawDataCell(Sender: TObject; const Rect: TRect;
Field: TField; State: TGridDrawState);
begin
if Table1.FieldByName('partno').AsFloat > 2500 then
DBGrid1.Canvas.Brush.Color := clGreen;
//DBGrid1.Canvas.Font.Color := clRed;
DBGrid1.DefaultDrawDataCell(Rect, Field, State);
end;
或(注意是"或")在OnDrawColumnCell下写:
二者的区别是OnDrawDataCell必须是Columns属性为csDefault,既为空。而OnDrawColumnCell没这个要求。
procedure TForm_cost.DBGrid1DrawColumnCell(Sender: TObject;
const Rect: TRect; DataCol: Integer; Column: TColumn;
State: TGridDrawState);
begin
with DBGrid1 do
begin
if ((State = [gdSelected]) or (State=[gdSelected,gdFocused])) then
begin
Canvas.Font.Color :=clLime;
Canvas.Brush.Color :=clMaroon;
end
else
if Table1.FieldByName('Salary').AsFloat < 40000 then
begin
canvas.Font.Color := clYellow;
canvas.Font.Style := [fsItalic];
Canvas.brush.Color := clGreen;
end
else
begin
canvas.Font.Color := clNavy;
Canvas.Font.Style := [fsBold];
Canvas.brush.Color := clRed;
end;
DefaultDrawColumnCell(Rect, DataCol, Column, State);
end;
//DBGrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State);
end;
procedure TForm1.DBGrid1DrawDataCell(Sender: TObject; const Rect: TRect;
Field: TField; State: TGridDrawState);
begin
if Table1.FieldByName('partno').AsFloat > 2500 then
DBGrid1.Canvas.Brush.Color := clGreen;
//DBGrid1.Canvas.Font.Color := clRed;
DBGrid1.DefaultDrawDataCell(Rect, Field, State);
end;
或(注意是"或")在OnDrawColumnCell下写:
二者的区别是OnDrawDataCell必须是Columns属性为csDefault,既为空。而OnDrawColumnCell没这个要求。
procedure TForm_cost.DBGrid1DrawColumnCell(Sender: TObject;
const Rect: TRect; DataCol: Integer; Column: TColumn;
State: TGridDrawState);
begin
with DBGrid1 do
begin
if ((State = [gdSelected]) or (State=[gdSelected,gdFocused])) then
begin
Canvas.Font.Color :=clLime;
Canvas.Brush.Color :=clMaroon;
end
else
if Table1.FieldByName('Salary').AsFloat < 40000 then
begin
canvas.Font.Color := clYellow;
canvas.Font.Style := [fsItalic];
Canvas.brush.Color := clGreen;
end
else
begin
canvas.Font.Color := clNavy;
Canvas.Font.Style := [fsBold];
Canvas.brush.Color := clRed;
end;
DefaultDrawColumnCell(Rect, DataCol, Column, State);
end;
//DBGrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State);
end;
本栏文章均来自于互联网,版权归原作者和各发布网站所有,本站收集这些文章仅供学习参考之用。任何人都不能将这些文章用于商业或者其他目的。( Pfan.cn )
【编程爱好者论坛】