我用StringGrid显示数据库种数据,当有一些特殊数据的时候,需要把StringGrid中的某几行的颜色设成特定的颜色,如何实现,谢谢。

解决方案 »

  1.   

    procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
    begin
      with StringGrid1 do
      with Canvas do
      begin
        FillRect(Rect);
        if StrToFloat(Cells[1,2]) > 2000 then  //如果数据大于2000则设置字体为红色
           Font.Color := clRed
        else Font.Color := clBlack;
        DrawText(Handle, PChar(Cells[ACol, ARow]), Length(Cells[ACol, ARow]),
                 Rect, DT_LEFT+DT_SINGLELINE+DT_VCENTER);
      end;
    end;
    end;
      

  2.   

    刚才多贴了一行end;!procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
    begin
      with StringGrid1 do
      with Canvas do
      begin
        FillRect(Rect);
        if StrToFloat(Cells[1,2]) > 2000 then  //如果数据大于2000则设置字体为红色
           Font.Color := clRed
        else Font.Color := clBlack;
        DrawText(Handle, PChar(Cells[ACol, ARow]), Length(Cells[ACol, ARow]),
                 Rect, DT_LEFT+DT_SINGLELINE+DT_VCENTER);
      end;
    end;
      

  3.   

    只能自已画了:
    将deafultDrawing设:false;在OnDrawCell写下:
    if ARow=2 then
    begin
      StringGrid1.Canvas.Brush.Color:=clRed;
      StringGrid1.Canvas.FillRect(Rect);
    end;
      

  4.   

    to things(平)(♂意难平♀) : 怎么我执行了你的代码,在StringGrid1中相应的那几行,变得什么都没有了。
      

  5.   

    StringGrid1.Canvas.FillRect(Rect);
    将清空单元格的值,
    加上
    TextOut(Rect.Left+2, Rect.Top+3, StringGrid1.Cells[ACol, ARow]);