Using a DBGrid in Lazarus

We followed the instructions in the Lazarus Wiki to display the data in a Microsoft Access table. This worked for our components.mdb database when we followed the supplementary instruction to change the SQLQuery1 property UsePrimaryKeyAsKey to False. We added each column by right clicking on Columns in the Object Inspector then selecting Add Item as shown below.

Adding a column

Adding a column

Having added the columns, we set their field names from the drop down list in the Object Inspector, changed some of the captions of the titles, emboldened the titles and set the column widths. The following screenshot shows the result.

Program DatabaseDemo in action

Program DatabaseDemo in action

The essential code of the files needed for program DatabaseDemo follows. You can download the three code files and the database in form_database_demo.zip. You will probably need to change the path of the database. The Microsoft Access page in the Lazarus Wiki explains how to create your own .dsn file if ours does not work for you.

We have emboldened important properties in the text view of the form.

uDatabaseDemo.pas

unit uDatabaseDemo;

interface

uses
  Classes, SysUtils, db,  odbcconn, sqldb, FileUtil, Forms, Controls,
  Graphics, Dialogs, DBGrids, DbCtrls;

type
  TfrmComponents = class(TForm)
    Datasource1: TDatasource;
    DBGrid1: TDBGrid;
    DBNavigator1: TDBNavigator;
    ODBCConnection1: TODBCConnection;
    SQLQuery1: TSQLQuery;
    SQLTransaction1: TSQLTransaction;
  end;
var
  frmComponents: TfrmComponents;

implementation

{$R *.lfm}

end.

uDatabaseDemo.lfm

object frmComponents: TfrmComponents
  Left = 300
  Height = 335
  Top = 232
  Width = 1101
  Caption = 'Delphi Components'
  ClientHeight = 335
  ClientWidth = 1101
  LCLVersion = '0.9.30'
  object DBGrid1: TDBGrid
    Left = 3
    Height = 256
    Top = 8
    Width = 1096
    Color = clWindow
    Columns = <    
      item
        Title.Caption = 'Type'
        Title.Font.Style = [fsBold]
        Width = 100
        FieldName = 'Name'
      end    
      item
        Title.Caption = 'Prefix'
        Title.Font.Style = [fsBold]
        Width = 100
        FieldName = 'Abbreviation'
      end    
      item
        Title.Caption = 'Palette'
        Title.Font.Style = [fsBold]
        Width = 100
        FieldName = 'Palette'
      end    
      item
        Title.Caption = 'Useful Properties'
        Title.Font.Style = [fsBold]
        Width = 300
        FieldName = 'UsefulProperties'
      end    
      item
        Title.Caption = 'Code Fragment'
        Title.Font.Style = [fsBold]
        Width = 450
        FieldName = 'Codefragments'
      end>
    DataSource = Datasource1
    TabOrder = 0
  end
  object DBNavigator1: TDBNavigator
    Left = 408
    Height = 25
    Top = 280
    Width = 241
    BevelOuter = bvNone
    ChildSizing.EnlargeHorizontal = crsScaleChilds
    ChildSizing.EnlargeVertical = crsScaleChilds
    ChildSizing.ShrinkHorizontal = crsScaleChilds
    ChildSizing.ShrinkVertical = crsScaleChilds
    ChildSizing.Layout = cclLeftToRightThenTopToBottom
    ChildSizing.ControlsPerLine = 100
    ClientHeight = 25
    ClientWidth = 241
    DataSource = Datasource1
    TabOrder = 1
    VisibleButtons = [nbFirst, nbPrior, nbNext, nbLast, nbRefresh]
  end
  object Datasource1: TDatasource
    DataSet = SQLQuery1
    left = 104
    top = 275
  end
  object ODBCConnection1: TODBCConnection
    Connected = True
    LoginPrompt = False
    KeepConnection = False
    Transaction = SQLTransaction1
    UserName = 'admin'
    FileDSN = 'E:\Database\components.dsn'
    left = 864
    top = 275
  end
  object SQLTransaction1: TSQLTransaction
    Active = True
    Action = caNone
    Database = ODBCConnection1
    left = 232
    top = 275
  end
  object SQLQuery1: TSQLQuery
    IndexName = 'DEFAULT_ORDER'
    Active = True
    Database = ODBCConnection1
    Transaction = SQLTransaction1
    ReadOnly = False
    SQL.Strings = (
        'SELECT * FROM ComponentDetails'
    )
    Params = <>
    UsePrimaryKeyAsKey = False
    left = 976
    top = 275
  end
end
        

DatabaseDemo.lpr

program DatabaseDemo;

uses
  Interfaces, Forms, uDatabaseDemo;

{$R *.res}

begin
  Application.Initialize;
  Application.CreateForm(TfrmComponents, frmComponents);
  Application.Run;
end.  
      

components.dsn

[ODBC]
DRIVER=Microsoft Access Driver (*.mdb)
UID=admin
UserCommitSync=Yes
Threads=3
SafeTransactions=0
PageTimeout=5
MaxScanRows=8
MaxBufferSize=2048
FIL=MS Access
DriverId=25
DefaultDir=E:\
DBQ=E:\components.mdb

Programming - a skill for life!

How to use a DBGrid to display and edit data in a database