Mega Code Archive

 
Categories / Delphi / Graphic
 

How to get different background color of DBGrid for odd and even rows (1)

Title: How to get different background color of DBGrid for odd and even rows (1) 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;