Friday, April 20, 2012

Delphi DBGrids.pas Source-Code mods for Alternating Row Color

Alternating DBGrid Row Colors in Borland / Embarcadero Delphi

As I migrate some existing internal software from Delphi to Dart Language and HTML/SVG/CSS, I figured I would post some of my soon-to-be "legacy" Delphi source code online for anyone that may want to leverage it. In this particular case, I have some code I use to rather automatically determine a reasonable color-coordinated alternating-color scheme for grid rows in a DBGrid, as well as the modifications I made to the DBGrids.pas to makes use of this color-flipping code.

UPDATE (JAN-2017): I used to host my modified Delphi DBGrids.pas source-code on another site, but I have now moved my source code onto this blog under this new entry: DBGrids.pas enhancement for automatic alternating grid row colors / highlights.  Go there for an update to all of this.

With my approach, I can have DBGrids with odd/even (i.e., alternating) rows having slightly different but yet nicely coordinated colors, and show their "selected" row as highlighted too

One approach: modify the DBGrids.pas source-code directly

I chose to modify the DBGrids.pas source code provided with Delphi, since the DBGrid.pas DrawCell (TCustomDBGrid.DrawCell method) provided no easy way to extend the routine to do what I wanted.  Fact is, I tried repeatedly to extend DBGrid via the DrawCell routines and such, but I just could not get the control I wanted.  It was so much easier to just "hack" the DBGrids.pas source code directly.

I went a bit beyond just alternating the grid-row colors, as I am also doing some other things like modifying how bookmarks work and selected-rows work and such.

I hope you can get some use of out this.  Enjoy!

Continue to read this Software Development and Technology Blog for computer programming articles (including useful free / OSS source-code and algorithms), software development insights, and technology Techniques, How-To's, Fixes, Reviews, and News — focused on Dart Language, SQL Server, Delphi, Nvidia CUDA, VMware, TypeScript, SVG, other technology tips and how-to's, plus my varied political and economic opinions.

No comments: