Marco Web Center

[an error occurred while processing this directive]

Home: Code Repository: Mastering Delphi 6

Chapter 17 - Project ClientRefresh

Project Structure

ClientRefresh.dpr
program ClientRefresh;

uses
  Forms,
  CliRefForm in 'CliRefForm.pas' {Form1};

{$R *.res}

begin
  Application.Initialize;
  Application.CreateForm(TForm1, Form1);
  Application.Run;
end.
CliRefForm.pas
unit CliRefForm;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, DB, DBClient, Grids, DBGrids, MConnect;

type
  TForm1 = class(TForm)
    cds: TClientDataSet;
    DCOMConnection1: TDCOMConnection;
    DataSource1: TDataSource;
    DBGrid1: TDBGrid;
    Button1: TButton;
    Button2: TButton;
    ListBox1: TListBox;
    Label1: TLabel;
    procedure Button1Click(Sender: TObject);
    procedure cdsAfterPost(DataSet: TDataSet);
    procedure cdsAfterScroll(DataSet: TDataSet);
    procedure Button2Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

type
  TMyGrid = class (TDBGrid)
  end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  if cds.ChangeCount = 0 then
    cds.Refresh;
end;

procedure TForm1.cdsAfterPost(DataSet: TDataSet);
begin
  cds.ApplyUpdates (-1);
end;

procedure TForm1.cdsAfterScroll(DataSet: TDataSet);
begin
  // refresh current record only
  if cds.UpdateStatus = usUnModified then
    cds.RefreshRecord;
  // log operation
  Listbox1.Items.Add (cds['Emp_no']);
end;

procedure TForm1.Button2Click(Sender: TObject);
var
  i: Integer;
  bm: TBookmarkStr;
begin
  // refresh visible rows
  cds.DisableControls;
  // start with the current row
  i := TMyGrid(DbGrid1).Row;
  bm := cds.Bookmark;
  try
    // get back t the first visible record
    while i > 1 do
    begin
      cds.Prior;
      Dec (i);
    end;
    // return to the current record
    i := TMyGrid(DbGrid1).Row;
    cds.Bookmark := bm;
    // go ahead until the grid is complete
    while i < TMyGrid(DbGrid1).RowCount do
    begin
      cds.Next;
      Inc (i);
    end;
  finally
    // set back everything and refresh
    cds.Bookmark := bm;
    cds.EnableControls;
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  cds.Active := True;
end;

end.
CliRefForm.dfm
object Form1: TForm1
  Left = 207
  Top = 125
  Width = 719
  Height = 248
  Caption = 'ClientRefresh'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  OnCreate = FormCreate
  PixelsPerInch = 96
  TextHeight = 13
  object Label1: TLabel
    Left = 552
    Top = 8
    Width = 61
    Height = 13
    Caption = 'Refresh Log:'
  end
  object DBGrid1: TDBGrid
    Left = 64
    Top = 40
    Width = 473
    Height = 169
    DataSource = DataSource1
    TabOrder = 0
    TitleFont.Charset = DEFAULT_CHARSET
    TitleFont.Color = clWindowText
    TitleFont.Height = -11
    TitleFont.Name = 'MS Sans Serif'
    TitleFont.Style = []
  end
  object Button1: TButton
    Left = 64
    Top = 8
    Width = 75
    Height = 25
    Caption = 'Refresh All'
    TabOrder = 1
  end
  object Button2: TButton
    Left = 144
    Top = 8
    Width = 97
    Height = 25
    Caption = 'Refresh Visible'
    TabOrder = 2
    OnClick = Button2Click
  end
  object ListBox1: TListBox
    Left = 552
    Top = 24
    Width = 137
    Height = 185
    ItemHeight = 13
    TabOrder = 3
  end
  object cds: TClientDataSet
    Aggregates = <>
    Params = <>
    ProviderName = 'DataSetProvider1'
    RemoteServer = DCOMConnection1
    AfterPost = cdsAfterPost
    AfterScroll = cdsAfterScroll
    Left = 16
    Top = 16
  end
  object DCOMConnection1: TDCOMConnection
    ServerGUID = '{C5DDE903-2214-11D1-98D0-444553540000}'
    ServerName = 'AppServTwo.RdmCount'
    Left = 16
    Top = 72
  end
  object DataSource1: TDataSource
    DataSet = cds
    Left = 16
    Top = 128
  end
end