改变当前行的颜色
在StringGrid1.Options中要包含goRowSelect
unit Unit1;interfaceuses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Grids;type
  TForm1 = class(TForm)
    StringGrid1: TStringGrid;
    procedure StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
  private
    { Private declarations }
  public
    { Public declarations }
  end;var
  Form1: TForm1;implementation{$R *.DFM}procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
begin
  with StringGrid1 do
    with Canvas do
    begin
      if (ARow=Row)and(ACol>=FixedCols) then
        Brush.Color:=clRed
      else
        if (ARow>=FixedRows)and(ACol>=FixedCols) then Brush.Color:=clWhite;
      FillRect(Rect);
    end;end;end.

解决方案 »

  1.   

    StringGrid改变某一CELL的背景颜色,CELL 中的字体可设置成不同 :
    procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
    begin
      with StringGrid1 do
      begin
        if (ACol = 3) and (ARow = 3) then
        begin
          Canvas.Brush.Color := clRed;
          Canvas.FillRect(Rect);
          Canvas.Font.Name := 'Courier New';
          Canvas.TextOut(Rect.Left + 2, Rect.Top + 2, Cells[ACol, ARow]);
        end;
      end;
    end;
      

  2.   

    先定义下面一个类:
    type TMyGrid=class(TStringGrid)
     end;假设该grid叫 StringGrid1 ,它的DefaultDrawing属性必须为true
    写它的OnDrawCell事件:if(ARow=2)and(ACol>0)then  //将第2行的背景变成红色
      begin
      StringGrid1.Canvas.Brush.Color:=clRed;
      StringGrid1.Canvas.FillRect(Rect);    //画红色
      StringGrid1.OnDrawCell:=nil;  //设置为nil,下面就不会无限递归
      try                           
        TMyGrid(StringGrid1).DrawCell(ACol,ARow,Rect,State);
       finally
        StringGrid1.OnDrawCell:=StringGrid1DrawCell; //恢复事件的地址
       end;
      end;