2008. augusztus 2., szombat

How to colour every second row in a TDBGrid green


Problem/Question/Abstract:

I want to make every other row in my TDBGrid green to simulate green bar paper.

Answer:

If you are using a TTable and either dBase or Paradox files, you can use the DataSet.RecNo to track which row is being painted:


procedure TForm1.DBGrid1DrawDataCell(Sender: TObject; const Rect: TRect;
  Field: TField; State: TGridDrawState);
const
  clSeaGreen = $00BAFF6F;
var
  OldBrushColor: TColor;
  OddRec: Boolean;
begin
  with TDBGrid(Sender) do
  begin
    OddRec := ((DataSource.DataSet.RecNo mod 2) <> 0);
    with Canvas do
    begin
      OldBrushColor := Brush.Color;
      if (gdSelected in State) then
        Brush.Color := clHighlight
      else if OddRec then
        Brush.Color := clSeaGreen
      else
        Brush.Color := clWhite;
    end;
    Canvas.FillRect(Rect);
    DefaultDrawDataCell(Rect, Field, State);
    Canvas.Brush.Color := OldBrushColor;
  end;
end;

Nincsenek megjegyzések:

Megjegyzés küldése