procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
  DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
  with (Sender as TDBGrid) do
  begin
    if ((State = [gdSelected]) or (State = [gdSelected, gdFocused])) then
    begin
      Canvas.Font.Color := clHighlightText;
      Canvas.Brush.Color := clHighlight;
    end
    else
    begin
      if DataSource.DataSet.RecNo mod 2 <> 0 then
        Canvas.brush.Color := $00F2F2F1
      else
        Canvas.brush.Color := clWhite;
    end;
    DefaultDrawColumnCell(Rect, DataCol, Column, State); 
  end;
end;

解决方案 »

  1.   

    设一个id 字段,类型位integer,自增型,增量为1,然后
    procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
      DataCol: Integer; Column: TColumn; State: TGridDrawState);
    begin
        if odd(table1.FieldByName('id').AsInteger) then
            begin
                    dbgrid1.Canvas.Font.Color:=clred;
                    dbgrid1.Canvas.Brush.color:=clyellow;
            end
            else
            begin
                    dbgrid1.Canvas.Font.Color:=clblue;
                    dbgrid1.Canvas.Brush.color:=clyellow;
                    end;        dbgrid1.DefaultDrawColumnCell(rect,datacol,column,state);
    end;
      

  2.   

    procedure TForm4.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
    var
      x,y:integer;
    begin
        x:=(acol-stringgrid1.LeftCol)*73+28;
        y:=(arow-stringgrid1.TopRow+1)*21+2;
      if (acol>0) and (arow>0) and
        not ([gdselected]<=state) then
      begin
        case Form1.Table.fieldstrclist[acol].fieldtype of
        'C':
          stringgrid1.Canvas.Font.Color:=clblack;
        'N':
          stringgrid1.Canvas.Font.Color:=clblue;
        'L':
          stringgrid1.Canvas.Font.Color:=clred;
        'D':
          stringgrid1.Canvas.Font.Color:=clfuchsia;
        end;
        stringgrid1.Canvas.TextRect(rect,x,y,
          stringgrid1.cells[acol,arow]);
      end;
      

  3.   

    Form1.Table.fieldstrclist[acol].fieldtype 
    可以是任何你想判断的变量