请问:如何将DBGRID查询的结果中值小于0的数显示为红色,其他为黑色

解决方案 »

  1.   

    onDrawColumnCelldrawtext分太少我就简单回答了.:)
      

  2.   

    为DBGrid1构件OnDrawDataCell事件编写响应程序:
      procedure TForm1.DBGrid1DrawDataCell(Sender: TObject; const Rect: TRect;Field: TField; State: TGridDrawState);
      begin
       if Table1.Fieldbyname(′Salary′).value<0 then
       DBGrid1.Canvas.Brush.Color:=ColorGrid1.ForeGroundColor
       DBGrid1.Canvas.Brush.Color:=ColorGrid1.BackGroundColor;
       DBGrid1.Canvas.FillRect(Rect);
       DBGrid1.Canvas.TextOut(Rect.left+2,Rect.top+2,Field.AsString);
      end;
      

  3.   

    procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
      DataCol: Integer; Column: TColumn; State: TGridDrawState);
    begin
      with DBGrid1.Canvas do
      if Column.FieldName = 'FieldName' then  // 要变颜色的字段名
        if Column.Field.Value < 0 then begin
          Font.Color := clRed;
          DBGrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State);
        end;
    end;
      

  4.   

    为DBGrid1构件OnDrawDataCell事件编写响应程序:if TableField.AsInteger < 0 then
      DBGrid.Canvas.Font.Color := clRed
    else
     DBGrid.Canvas.Font.Color := clBlack;
    DBGrid.DefaultDrawColumnCell(Rect, DataCol, Column, State);