代码如下:
| 在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; |

