Get different background color of DBGrid for odd and even rows
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState); var test1: Real; RowNo: Integer; begin with (Sender as TDBGrid) do begin if (gdSelected in State) then begin // color of the focused row Canvas.Brush.Color := clblue; end else begin // get the actual row number rowno := Query1.RecNo; // odd or even ? test1 := (RowNo / 2) - trunc(RowNo / 2); // If it's an even one... if test1 = 0 then begin farbe := clWhite end // ...else it's an odd one else begin farbe := clYellow; end; Canvas.Brush.Color := farbe; // font color always black Canvas.Font.Color := clBlack; end; Canvas.FillRect(Rect); // manualy output the text Canvas.TextOut(Rect.Left + 2, Rect.Top + 1, Column.Field.AsString); end end; |