在OnDrawColumnCell事件中实现 if Column.FieldName = 'ID' then begin if (state = [gdSelected]) or (state = [gdFocused, gdselected]) then begin DBGridEh1.Canvas.Font.Color := clWhite; DBGridEh1.Canvas.Brush.Color := clNavy; end else begin DBGridEh1.Canvas.Font.Color := clBlack; DBGridEh1.Canvas.Brush.Color := clSkyBlue; //clBtnFace; end; end
先设置DBGRID1的DefaultDrawing属性为:False,然后在DBGrid1的OnDrawColumnCell事件中写入下列代码。 procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState); begin if ADODataSet1.FieldByName('Matl').AsFloat<0.1 then DBGrid1.Canvas.Font.Color := clRed else DBGrid1.Canvas.Font.Color := clBlack; DBGrid1.Canvas.FillRect(Rect); DBGrid1.Canvas.TextOut(Rect.Left+2,Rect.Top+2,Column.Field.AsString); end;
if Column.FieldName = 'ID' then
begin
if (state = [gdSelected]) or (state = [gdFocused, gdselected]) then
begin
DBGridEh1.Canvas.Font.Color := clWhite;
DBGridEh1.Canvas.Brush.Color := clNavy;
end
else
begin
DBGridEh1.Canvas.Font.Color := clBlack;
DBGridEh1.Canvas.Brush.Color := clSkyBlue; //clBtnFace;
end;
end
procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
if ADODataSet1.FieldByName('Matl').AsFloat<0.1 then
DBGrid1.Canvas.Font.Color := clRed
else
DBGrid1.Canvas.Font.Color := clBlack;
DBGrid1.Canvas.FillRect(Rect);
DBGrid1.Canvas.TextOut(Rect.Left+2,Rect.Top+2,Column.Field.AsString);
end;