请问:如何在DBGrid控件实现每行数据的颜色不一样?

解决方案 »

  1.   

    procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
      DataCol: Integer; Column: TColumn; State: TGridDrawState);
    begin
     Case DataCol Mod 2 = 0 of
     True: DbGrid1.Canvas.Brush.Color:= clBlue;
     False: DbGrid1.Canvas.Brush.Color:= clAqua;
     End;
     If ((State = [gdSelected]) or (State=[gdSelected,gdFocused])) then
     If Not DbGrid1.SelectedRows.CurrentRowSelected then
     DbGrid1.Canvas.Brush.Color:=clRed;
      DbGrid1.Canvas.Pen.Mode:=pmMask;
      DbGrid1.DefaultDrawColumnCell (Rect, DataCol, Column, State);
    end;
      

  2.   

    还有这种样式:
    procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
      DataCol: Integer; Column: TColumn; State: TGridDrawState);
    begin
    Case adoTable1.RecNo mod 2 = 0 of
     True : DbGrid1.Canvas.Brush.color:=clAqua; //偶数行用浅绿色显示
     False: DbGrid1.Canvas.Brush.color:= clcream; //奇数行用蓝色表示
     end;
     If ((State = [gdSelected]) or (State=[gdSelected,gdFocused])) then
     Case DataCol mod 2 = 0 of
     True : DbGrid1.Canvas.Brush.color:=clRed;
     False: DbGrid1.Canvas.Brush.color:= clGreen;
     end;
     DbGrid1.Canvas.pen.mode:=pmMask;
     DbGrid1.DefaultDrawColumnCell (Rect, DataCol, Column, State);