Using a DBGrid in Delphi

We added to our form a DGGrid and a DBNavigator from the Data Controls tab, a DataSource from the Data Access tab and an ADOTable from the ADO tab. The DBGrid acts as both a connection to a database and a dataset for a single table. The ADOTable page on the AsIPlease website shows you how to set up the connection string. The 'hooks' between the components are easy to set up using the Object Inspector. See uDatabase.frm below for the settings.

Program ADO_Demo in action

Program ADO_Demo in action

The essential code of the files needed for program ADO_Demo follows. You can download the three code files, our components.dsn file and the database in form_ado_demo.zip

uDatabase.pas

unit uDatabase;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, DB, ADODB, Grids, DBGrids, ExtCtrls, DBCtrls;

type
  TfrmComponents = class(TForm)
    dsComponents: TDataSource;
    DBGrid1: TDBGrid;
    adotComponents: TADOTable;
    DBNavigator1: TDBNavigator;
  end;

var
  frmComponents: TfrmComponents;

implementation

{$R *.dfm}

end.

uDatabase.dfm

object frmComponents: TfrmComponents
  Left = 296
  Top = 141
  Width = 1031
  Height = 271
  Caption = 'Components'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -14
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 120
  TextHeight = 16
  object DBGrid1: TDBGrid
    Left = 4
    Top = 9
    Width = 1005
    Height = 152
    DataSource = dsComponents
    TabOrder = 0
    TitleFont.Charset = DEFAULT_CHARSET
    TitleFont.Color = clWindowText
    TitleFont.Height = -14
    TitleFont.Name = 'MS Sans Serif'
    TitleFont.Style = []
    Columns = <
      item
        Expanded = False
        FieldName = 'Name'
        Title.Caption = 'Type'
        Title.Font.Charset = DEFAULT_CHARSET
        Title.Font.Color = clWindowText
        Title.Font.Height = -14
        Title.Font.Name = 'MS Sans Serif'
        Title.Font.Style = [fsBold]
        Width = 100
        Visible = True
      end
      item
        Expanded = False
        FieldName = 'Abbreviation'
        Title.Caption = 'Prefix'
        Title.Font.Charset = DEFAULT_CHARSET
        Title.Font.Color = clWindowText
        Title.Font.Height = -14
        Title.Font.Name = 'MS Sans Serif'
        Title.Font.Style = [fsBold]
        Width = 100
        Visible = True
      end
      item
        Expanded = False
        FieldName = 'Palette'
        Title.Font.Charset = DEFAULT_CHARSET
        Title.Font.Color = clWindowText
        Title.Font.Height = -14
        Title.Font.Name = 'MS Sans Serif'
        Title.Font.Style = [fsBold]
        Width = 80
        Visible = True
      end
      item
        Expanded = False
        FieldName = 'UsefulProperties'
        Title.Caption = 'Useful Properties'
        Title.Font.Charset = DEFAULT_CHARSET
        Title.Font.Color = clWindowText
        Title.Font.Height = -14
        Title.Font.Name = 'MS Sans Serif'
        Title.Font.Style = [fsBold]
        Width = 281
        Visible = True
      end
      item
        Expanded = False
        FieldName = 'Codefragments'
        Title.Caption = 'Code fragments'
        Title.Font.Charset = DEFAULT_CHARSET
        Title.Font.Color = clWindowText
        Title.Font.Height = -14
        Title.Font.Name = 'MS Sans Serif'
        Title.Font.Style = [fsBold]
        Width = 498
        Visible = True
      end>
  end
  object DBNavigator1: TDBNavigator
    Left = 376
    Top = 184
    Width = 240
    Height = 25
    DataSource = dsComponents
    TabOrder = 1
  end
  object dsComponents: TDataSource
    DataSet = adotComponents
    Left = 184
    Top = 168
  end
  object adotComponents: TADOTable
    Active = True
    ConnectionString = 
        'Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\Components.mdb;P' +
      'ersist Security Info=False'
    CursorType = ctStatic
    TableName = 'ComponentDetails'
    Left = 224
    Top = 168
  end
end

ADODemo.dpr

program ADO_Demo;

uses
  Forms,
  uDatabase in 'uDatabase.pas' {frmComponents};

{$R *.res}

begin
  Application.Initialize;
  Application.CreateForm(TfrmComponents, frmComponents);
  Application.Run;
end.
Programming - a skill for life!

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