All Delphi

Everything You Want To Know From VCL, Tips To Snippet All Everything About Delphi And CodeGear Related

    Search

  • Sponsors

  • Spottt
    Spottt

How To Coloring DBGRID Row

12th July 2008

hi all delphi readers, You’ve seen this surely on web pages. Alternating table row colors means displaying the first record in one color and the second record in another color and continue to alternate the color of each row displayed.

When working with datasets with many rows, alternating the background color of each row can increase readability.
Alternating DBGrid Row Colors

Here’s the OnDrawColumnCell event handler for a DBGrid control to color every second row in a different color.
Sample 1

    procedure TGridForm.DBGridDrawColumnCell(
        Sender: TObject;
        const Rect: TRect;
        DataCol: Integer;
        Column: TColumn;
        State: TGridDrawState) ;
    var
      grid : TDBGrid;
      row : integer;
    begin
      grid := sender as TDBGrid;
      row := grid.DataSource.DataSet.RecNo;

      if Odd(row) then
        grid.Canvas.Brush.Color := clSilver
      else
        grid.Canvas.Brush.Color := clDkGray;

      grid.DefaultDrawColumnCell(Rect, DataCol, Column, State) ;
    end; (* DBGrid OnDrawColumnCell *)

Note that the row color eliminates the need of table borders and make it easy for the eye to read a row. In a vertical sense, the colors make it easier to ‘catch’ an item because it is on either one of the colors.

Of course, you need to take into consideration the color of the selected cell/row - I’ll leave this up to you - but this should help: Coloring Selected Rows

Play with the Options property to have the best looking grid for your Delphi application :)

Rate this:
3.7 (1 person)

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>