Bayesean Blog - Desktop, Mobile and IOT Developer Blog


Delphi A Professional VCL DBGrid Part Two

Posted on 19th Apr 2019 in Delphi VCL


part2header31.png

 

 Background

Following on from Part One you will learn to add row numbers and then seamlessly integrate components into the DBgrid.

The following are covered in this blog:

  • Add row numbers to the Grid
  • Add a DblookupCombo to the column
  • Add the Windows 10 datepicker to the column

Add Row Numbers to the Grid

I have added this in although there are many sites that share similar code on Google.

This adds the row numbers in a column with no data.

Usage

RowNumbers(Rect,dbgrid1,datacol);

Code

 

procedure RowNumbers(Rect:TRect;grid:Tdbgrid;datacol:integer);

 begin

 //set row numbers

     if grid.datasource.DataSet.RecNo > 0 then

      begin

      if Datacol = 0 then

      Grid.Canvas.TextOut(Rect.Left + 6, Rect.Top, IntToStr(grid.DataSource.DataSet.RecNo));

      end;

 end;

  Add a DblookupCombo to the column

The first step is to add a dbcombobox onto the dbgrid anywhere. Link to a data.

In the formcreate event the dblookupcombobox requires the parent to be the Dbgrid and is kept invisible.

 

procedure TForm4.FormCreate(Sender: TObject);

var

 StyleName: string;

begin

dblookupcombobox1.Parent:= dbgrid1;

 dblookupcombobox1.visible:= false;

end;

 Add the procedure DBlookupCombodrawcolumncell which positions the dblookupcombobox in the correct column and makes it visible for the row selected.

Usage

 

DBlookupCombodrawcolumncell(dblookupcombobox1,datacol,5,Column,dbgrid1,

Rect,State)

Code

 

procedure DBlookupCombodrawcolumncell (combo:TdblookupCombobox; datacol,colpos:integer; Column: TColumn;  Grid: TDBGrid;  const Rect: TRect;  State: TGridDrawState);

var

 datarect:TRect;

 begin

   if not (gdfixed in state) and (gdFocused in State) and (Datacol = colpos) then

   begin

   combo.Font.Size:= grid.Font.Size ;

  if grid.options = grid.options + [dgindicator] then

   DataRect := TModDbGrid(grid).CellRect((colpos+1),TModDbGrid(grid).row)

   else

   DataRect := TModDbGrid(grid).CellRect((colpos),TModDbGrid(grid).row);

   Inflaterect(datarect,-1,-1);

   combo.Width := DataRect.Width +2;

   combo.Left := DataRect.right - combo.width;

   combo.Top := DataRect.top -2;

   combo.Height := DataRect.bottom-DataRect.top;

   end;

 end;

 

The procedure  DBGrid1CellClick event is used to set focus once clicked on the cell for the column that the fieldname is assigned.

Usage

dblookupComboCell(DBLookupComboBox1, DBGrid1, Column, 'STATE')

Code

 

 

begin

  // remove editing cannot post here but will need to globally post change

  if (Column.FieldName = FieldName) then

    combo.SetFocus;

 

The procedure  DBGrid1ColEnter event is used to make the DBCombobox visible.

 Usage

DBlookupComboColenter(DBLookupComboBox1, 'STATE', 5, DBGrid1);

 Code

  

procedure DBlookupComboColenter(combo: Tdblookupcombobox; fieldname:string;

colpos:integer;grid:Tdbgrid);

 begin

  if Grid.Selectedindex = colpos then

  begin

    combo.Visible := True;

  end;

 end;

 

The procedure  DBGrid1ColExit event is used to make the DBCombobox invisible.

Usage

DBlookupComboColExit(DBLookupComboBox1, 'STATE', 5, DBGrid1);

 Code

  procedure DBlookupComboColExit(combo:Tdblookupcombobox; fieldname:string;colpos:integer;grid:Tdbgrid);

 

begin

  if Grid.Selectedindex = colpos then

  combo.Visible := False;

 end;

 Add the Windows 10 Datepicker to the column

The first step is to place the datepicker anywhere on the dbgrid. Then in the Formcreate event set the datepicker’s parent to the dbgrid and set the datapicker to be invisible.

 

 

procedure TForm4.FormCreate(Sender: TObject);

var

  StyleName: string;

begin

   DatePicker1.Parent := DBGrid1;

  DatePicker1.visible := false;

 In the DBGridDrawColumnCell the procedure DatePickerdrawcolumncell is inserted which will adjust font sizes and set the position of the datepicker within the active column

 Usage

DatePickerDrawColumnCell(datepicker1,datacol,6,Column,dbGrid1,Rect,State);

Code

p

rocedure DatePickerdrawcolumncell(picker:TDatePicker;datacol,colpos:integer;Column: TColumn; Grid: TDBGrid; const Rect: TRect; State: TGridDrawState);

 var

 Datarect:TRect;

 begin

   if not (gdfixed in state) and (gdFocused in State) and (Datacol = colpos) then

  begin

     picker.Font.Size:= grid.Font.Size ;

    if grid.options = grid.options + [dgindicator] then

    DataRect := TModDbGrid(grid).CellRect((colpos+1),TModDbGrid(grid).row)

    else

      DataRect := TModDbGrid(grid).CellRect((colpos),TModDbGrid(grid).row);

     Inflaterect(datarect,-1,-1);

     picker.Width := DataRect.Width +2;

     picker.Left := DataRect.right - picker.width+1;

     picker.Top := DataRect.top -2;

     picker.Height := DataRect.bottom-DataRect.top;

    picker.Date:= dateof(grid.DataSource.dataset.fieldbyname(grid.Columns[colpos].fieldname).asdatetime);

   end;

   end;

 To  ensure that the live datepicker receives changes from the datasource. The DatepickerCell procedure is placed in the DBGridCellClick event.

 Usage

DatepickerCell(DatePicker1, DBGrid1, Column, 'BIRTH_DATE');

 Code

 

procedure DatepickerCell(picker:Tdatepicker;grid:Tdbgrid;Column: TColumn; fieldname:string);

 begin

 //remove editing cannot post here but will need to globally post change

  if (Column.FieldName = fieldname) then

    begin

   //  grid.options:= grid.options - [dgediting];

    picker.Date:= dateof(grid.DataSource.DataSet.fieldbyname(fieldname).asdatetime);

     end;

   end;

 In the DBcolEnter the datapickerColenter procedure is used to populate the Datepicker with the correct data from the highlighted row. It also ensures that the datepicker is visible.

 Usage

DatePickerColenter(DatePicker1, 'BIRTH_DATE', 6, DBGrid1);

Code

 

procedure DatePickerColenter(picker:Tdatepicker;fieldname:string;colpos:integer;grid:Tdbgrid);

 begin

  if grid.SelectedIndex = colpos then

    begin

    picker.Visible := True;

 //   picker.Date:= grid.DataSource.DataSet.FieldByName(fieldname).AsDateTime;

    end;

 end;

 In the DBcolExit the datapickerColExit procedure is used to make the Datepicker invisible.

 Usage

 

DatepickerColExit(DatePicker1, 'BIRTH_DATE', 6, DBGrid1);

 Code

 

procedure DatepickerColExit(picker:Tdatepicker;fieldname:string;colpos:integer;grid:Tdbgrid);

  begin

   if Grid.SelectedIndex = colpos then

  Picker.Visible := False;

      end;

 To ensure that the live datepicker sends changes from the datasource. The DatepickerKeyPress procedure is placed in the DBGridCellClick event.

Usage

DatepickerKeyPress(Sender, DatePicker1, Key, 'BIRTH_DATE', DBGrid1);

Code

 

procedure DatepickerKeyPress(Sender: TObject;picker:TDatePicker;var Key: Char;fieldname:string;grid:TDbgrid);

 begin

  if (key = Chr(9)) then Exit;

   if (Grid.SelectedField.FieldName = fieldname) then

  begin

    picker.SetFocus;

    SendMessage(picker.Handle, WM_Char, word(Key), 0);

    end;

   grid.DataSource.DataSet.FieldByName(fieldname).AsDateTime:= picker.Date;

 end;

To update the database when the changes have been, the Windowsdatepickers onchange event is used to edit the datasource and then post the data.

As the dbgrid is normally in a non-edit mode the grid option needs to be changed by using a originalgridoptions variable to allow editing.

 Code

procedure TForm4.DatePicker1Change(Sender: TObject);

begin

  DBGrid1.options := originalgridoptions;

  DBGrid1.DataSource.DataSet.Edit;

  DBGrid1.DataSource.DataSet.FieldByName('BIRTH_DATE').AsDateTime :=

    dateof(DatePicker1.Date);

  DBGrid1.DataSource.DataSet.post;

end;

Code Repository

Feel Free to download the code that covers all four parts here

Download Code

 

 

 


Lauro      Commented   6 months ago Reply

Congratulations on the Code! This example can be used in DBGrid to make the Tree feature. | Customers A | -Sales $ 100.00

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