Bayesean Blog - Desktop, Mobile and IOT Developer Blog


Delphi A Professional VCL DBGrid Part One

Posted on 22nd Mar 2019 in Delphi VCL


part1header20.png

Background

This is Part One of a Four Part Series on enhancing a DBgrid which deals with the following:

  • Setting the generic background within the Grid.
  • Set alternate rRow Colors for the Grid.
  • Set column colors by column number.
  • Add styled checkboxes to a column.
  • Allow checkbox column to highlight a row for a prescribed color.
  • Set active row highlight.

 

All of the above have procedures that reside in a DBGridUtils unit that can be reused in your application by including it in as part of the users unit and by adding the DBGrids event procedure onDrawColumnCell.

procedure DBGrid1DrawColumnCell(Sender: TObject;

      const Rect: TRect;  DataCol: Integer;  Column: TColumn;

      State: TGridDrawState);

All code discussed will be found at the end of the series with a useful example.

Setting the Generic Background within the Grid

The Generic Background of the DBGrid is white and looks unpleasant in a styled application especially if the grid has one or two lines and there is no way of adjusting the standard DBGrid.

To change a new type TmodDBgrid is declared to allow access to the customGrid which in turn allows access to the FinternalColor. This we set using the styleservice.

 

Type

TModDbGrid = class(TCustomGrid)

procedure SetGenericBackground(sender:Tobject);

 begin

 //Change color if the white space below the grid to same as background

 TModDBGrid(sender).FInternalColor:= styleservices.GetStyleColor(scCategoryPanelGroup);

 end;

 Set Alternate Row Colors for the Grid

There are many source code snippets in Google but this code includes the styling that is required.

Here the usage code sets the alternate rows from column 0 to column 11 and (Datacol , Column  the grid name, Rect and State) of the DBGrid1DrawColumnCell procedure.

Usage

SetAutoAlternateColorLines(0,11,Datacol,Column,dbgrid1,Rect,State);

 Code

 

procedure SetAutoAlternateColorLines(const fromcol:integer;

const toCol:integer;Datacol:integer;Column: TColumn; Grid: TDBGrid;

const Rect: TRect; State: TGridDrawState);

 begin

 if ((Datacol >= fromcol) and (Datacol <= tocol)) then

      begin

       if Grid.DataSource.DataSet.RecNo mod 2 = 0 then

        begin

          if styleservices.Enabled  then

            begin

             Grid.Canvas.Brush.Color := styleservices.GetStyleColor(scCategoryPanelGroup);

             Grid.Canvas.Font.Color  := styleservices.GetStylefontColor(sfButtonTextnormal);

           end;

        end

        else

        begin

           //leave blank for generic background

        end;

       Grid.DefaultDrawColumnCell(Rect,Column.Index,Column,State);

     end;

  end;

 

Set Column colors by column number

The Colnumber is the column number which starts from 0 to column n-1, the colcolor is the color required. This allows a column to be highlighted by using a specific color.

Usage

SetColumnColor(Sender, 1, DataCol, clMedGray, DBGrid1, Rect, Column, State);

 Code

 

procedure setcolumncolor(sender:TObject; colnumber,datacol:integer;

colcolor:Tcolor; grid:Tdbgrid; Rect:Trect;Column: TColumn;

State: TGridDrawState);

 var

  vdbgrid:TDBgrid absolute sender;

 begin

  if (datacol = colnumber) then

      begin

        vdbgrid.Canvas.brush.Color:= colcolor;

        vdbGrid.Canvas.Font.Color  := styleservices.GetStylefontColor(sfButtonTextNormal);

      end;

       if (gdSelected in State) then

       with grid.canvas do

        begin

        Brush.Color:= styleServices.GetSystemColor(clHighlight);

        end;

    TDBgrid(sender).DefaultdrawColumnCell(rect,datacol,Column,State);

  end;

 

Add styled checkboxes to a column

Most checkbox add-on’s use an added checkbox component. Unfortunately this tends to show the checkbox in only the highlighted row.

This method draws the checkbox’s and styled these checkbox’s correctly for all the rows ensuring that all checkboxes are visual.

The Fieldname in string format is required so that the correct checkbox state is reflected. Valuecheck is unchecked for 0 and 1 is checked

 

Usage

CheckBoxDrawColumnCell('RISK_LEVEL','1', dbGrid1, Rect, Column, State);

 Code

 

procedure CheckBoxdrawcolumncell(FieldName: String;

ValueCheck: Variant; Grid: TDBGrid; const Rect: TRect;

Column: TColumn; State: TGridDrawState);

const

  CtrlState: Array[Boolean] of Integer = (DFCS_BUTTONCHECK, DFCS_BUTTONCHECK or DFCS_CHECKED);

  CtrlStateXP: Array[Boolean] of TThemedButton = (tbCheckBoxUncheckedNormal, tbCheckBoxCheckedNormal);

var

  R: TRect;

  Details: TThemedElementDetails;

begin

  if Column.FieldName = FieldName then

  begin

    Column.Title.Alignment := taCenter;

    Column.Alignment := taCenter;

    Grid.Canvas.FillRect(Rect);

 

   if StyleServices.Enabled  then

    begin

    Details := StyleServices.GetElementDetails(CtrlStateXP[Column.Field.Value = ValueCheck]);

    StyleServices.DrawElement(Grid.Canvas.Handle, Details, Rect);

    if ((not (gdFixed in State)) and (not (gdselected in state))and (not (gdFocused in State) ))then

     begin

        Grid.Canvas.Brush.Color := StyleServices.GetSystemColor(clHighlight);

           DrawFrameControl(Grid.Canvas.Handle, R, DFC_BUTTON,CtrlState[Column.Field.Value = ValueCheck]);

     end;

    end

    else

    begin

      R := Rect;

      InflateRect(R, -1, -1);

          DrawFrameControl(Grid.Canvas.Handle, R, DFC_BUTTON, CtrlState[Column.Field.Value = ValueCheck]);

       end;

 end;

end;

 

Allow checkbox Column to highlight a row for a prescribed color

This allows the user to check a row which will highlight to a set color.

It uses the Fieldname in string format to determine the checked state.

Booleantype 0 sets the checkbox to empty and 1 fulls the checkbox.

Highlight color is set ‘fromcol’ to ‘tocol’.

Highlight color is set using highlightcol.

 Usage

CheckboxHighlightRows(sender,'RISK_LEVEL','1',clWebLightCoral,0,11,datacol,dbgrid1,Rect,Column,State) ;

 Code

 

procedure CheckboxHighlightrows(sender:Tobject;Fieldname:string;

booleantype:string; highlightcol:tcolor;Fromcol,ToCol:integer;

datacol:integer;grid:Tdbgrid;Rect:Trect;Column: TColumn;

State: TGridDrawState);

 begin

  if ((Datacol >= fromcol) and (Datacol <= tocol)) then

      begin

      if booleantype = '1' then

      begin

        if grid.datasource.DataSet.FieldByName(Fieldname).asinteger = 1 then

         grid.Canvas.Brush.Color := StyleServices.GetSystemColor(highlightcol);

      end

        else

      if lowercase(booleantype) = 'true' then

       begin

        if grid.datasource.DataSet.FieldByName(Fieldname).AsBoolean then

        grid.Canvas.Brush.Color := StyleServices.GetSystemColor(highlightcol);

       end

     end;

  TDBgrid(sender).DefaultdrawColumnCell(rect,datacol,Column,State);

 end;

 

Set Active Row Highlight

By adding many of the above procedures, the active row highlight has been overwritten.

To reintroduce this option, Set the columns that requires the highlighted color.

The Highlight color is set ‘fromcol’ to ‘tocol’.

The Highlight color is preset and yes by including a different color it will be overridden.

 Usage

SetActiveRowHighlight(dbgrid1,datacol,0,11,column, Rect,State,clhighlight );

Code

 

procedure SetActiveRowHighlight(grid:Tdbgrid;datacol,fromcol,

tocol:integer;Column: TColumn;  const Rect: TRect;

State: TGridDrawState;const rowcolor:Tcolor = clhighlight);

  var

  modgrid:TMODdbgrid;

  begin

 if ((Datacol >= fromcol) and (Datacol <= tocol)) then

      begin

       if Grid.DataSource.DataSet.RecNo = Modgrid.Row then

        begin

          if styleservices.Enabled  then

            begin

            if rowcolor = clhighlight then

             begin

             Grid.Canvas.Brush.Color := styleservices.GetSystemColor(clhighlight);

             Grid.Canvas.Font.Color  := styleservices.GetStylefontColor(sfButtonTextnormal);

             end

             else

             begin

             Grid.Canvas.Brush.Color := rowcolor;

            Grid.Canvas.Font.Color  := styleservices.GetStylefontColor(sfButtonTextnormal);

             end;

           end;

        end

        else

        begin

           //leave blank for generic background

        end;

       Grid.DefaultDrawColumnCell(Rect,Column.Index,Column,State);

     end;

  end;

 

Download the code from Github that relates to all Four Parts.

 

Download Code

 

 


Be the First to Comment...

Add a Comment

9+5

Recent News

Delphi Delimited String to Fields
Delphi A Professional VCL DBGrid Part Four
Delphi A Professional VCL DBGrid Part Three
Delphi A Professional VCL DBGrid Part Two
Delphi A Professional VCL DBGrid Part One
Delphi VCL Buttons in DBGrid
Two Helper Apps for Delphi LibUSB
Delphi Libusb Library Introduction

Categories

Bootstrap 4
Delphi VCL
Delphi FMX
Ajax
Bootstrap 3
CSS
XE4>Delphi > XE4
Delphi < XE4
PHP

Archives

August 2019

Delphi Delimited String to Fields

June 2019

Delphi A Professional VCL DBGrid Part Four

May 2019

Delphi A Professional VCL DBGrid Part Three

April 2019

Delphi A Professional VCL DBGrid Part Two

March 2019

Delphi A Professional VCL DBGrid Part One

November 2018

Delphi VCL Buttons in DBGrid

October 2018

Two Helper Apps for Delphi LibUSB

September 2018

Delphi Libusb Library Introduction

August 2018

Delphi Object directly to a Json string in a REST Client
Delphi using Environment Variables in your App

July 2018

Delphi FMX Leaflet Plotter using OSM Maps

June 2018

C2PAS32 Convertor Application
C to Delphi Open Source Convertors Shootout
Delphi command-line programs with DOSCommand

May 2018

Delphi PDF Embedded viewer with PDF.js

March 2018

Delphi FMX - Changing TCharacter to TCharHelper
Make Your Delphi App POP using Javascript!

January 2018

Delphi FMX Dashboard using Chart.JS
Delphi FMX Form Docking

December 2017

PHP Slim REST Server & Delphi Auth Part 5

November 2017

Delphi FMX REST Client App Part 4

October 2017

Delphi VCL REST Pricing Client App Part 3

September 2017

Delphi REST VCL Client Basic Auth Part 2B

August 2017

Delphi REST Client Part 2A
PHP PDO REST Server Part 1

July 2017

PHP REST Server and Delphi Client Intro

June 2017

Delphi SQLite Encryptor-Decryptor Tool
Updating Applications Manifest using Delphi

May 2017

Create a Visual IP Address Geolocation with PHP

March 2017

PHP Downloader using Countdown timer
PHP File Downloader from a Inbox Selection

February 2017

Javascript Image-File Uploader with ThumbViewer

January 2017

Morris Charts and PHP-PDO

December 2016

CSS to create a functional Toggle Button