Reto Höhener
Reto Höhener

Reputation: 5808

What is the standard way in delphi 5 to watch for a selection change in a DBGrid?

I have many "master/detail" forms in my application. A TDBGrid where each row shows a few core values of the item. Below the grid is usually a "Detail area" that shows the complete information of the item that is currently selected in the grid.

Currently I am listening to the "AfterScroll"-event of the TADOQuery behind the grid, but it seems to give me too many events.

Is AfterScroll the correct event for this? How are you doing this?

Upvotes: 2

Views: 594

Answers (2)

Disillusioned
Disillusioned

Reputation: 14832

The "standard" way (in a data-aware environment) would be to not control that using GUI controls, but rather using the data components.

Most of the table data sets provide MasterSource (linked to an appropriate TDataSource component), and MasterFields properties. You use these to link your datasets in a master-detail relationship. Then your detail grid (or other data-aware controls) only needs to concern itself with linking to the correct dataset.

EDIT
Other kinds of datasets (e.g. TQuery, TADOQuery) sometimes provide DataSource to be used for a similar purpose. From Delphi 5 help: "Set DataSource to automatically fill parameters in a query with fields values from another dataset."
However, there are quite a few more complications (as will be observed reading the help). So it may be advisable to use TTable or TADOTable for the detail dataset instead.

Upvotes: 4

Sertac Akyuz
Sertac Akyuz

Reputation: 54802

I'm not aware if there's any 'standard' way but IMO AfterScroll is fine. Use a timer to prevent updating controls in quick succession, for instance while scrolling the grid. An example:

procedure TSomeForm.DataSetAfterScroll(DataSet: TDataSet);
begin
  if not DataSet.ControlsDisabled then begin
    if ScrollTimer.Enabled then
      ScrollTimer.Enabled := False;
    ScrollTimer.Enabled := True;
  end;
end;

procedure TSomeForm.ScrollTimerTimer(Sender: TObject);
begin
  ScrollTimer.Enabled := False;
  UpdateGUI;
end;

I think you'll find an interval of 250-300 ms pleasant.

Upvotes: 1

Related Questions