Sorting the records
In order to have the UFO sightings sorted by field "ID", select cdsUfo in the Design view.
In the Object Inspector, next to IndexFieldNames, type: ID
This creates an "index file" in memory that specifies the order of the records. Note that this index is not
saved to disk when the database is closed, it is re-created each time the database is opened.
Data entry
Entering data directly in the DBGrid is not very safe, because the data is not verified before storing it.
Let's improve on this.
Firstly, we don't want the user to change data directly in the grid:
in the Object Inspector, set property AutoEdit of dsUfo to False.
Next, we add a panel with a few data controls.
- Drop a TPanel somewhere below the DBGrid.
Set its property Align to alBottom.
- Drop a DBMemo on this panel.
In the Object Inspector, set its property DataSource to dsUfo and set property DataField to NOTES.
- Below the DBMemo, drop another panel. Set its Name to panEdit and the property Visible to False.
- Drop 13 TDBEdit components onto the panel panEdit.
Set the property DataSource of all these edit-boxes to dsUfo.
Set the other properties as follows:
Component | Name | DataField |
TDBEdit1 | edID | ID |
TDBEdit2 | edDesc | DESC |
TDBEdit3 | edLocation | LOCATION |
TDBEdit4 | edCountry | COUNTRY |
TDBEdit5 | edCE | CE |
TDBEdit6 | edSource | SOURCE |
TDBEdit7 | edDurat | DURAT |
TDBEdit8 | edWit | WIT |
TDBEdit9 | edShape | SHAPE |
TDBEdit10 | edAppear | APPEAR |
TDBEdit11 | edSize | SIZE |
TDBEdit12 | edOcc | OCC |
TDBEdit13 | edLink2 | LINK2 |
- Below the DBEdits, add two buttons. Name the first one btnSave and the other one btnCancel.
- Add OnClick event handlers for the buttons:
procedure TForm1.btnSaveClick(Sender: TObject);
begin
cdsUfo.Post;
end;
procedure TForm1.btnCancelClick(Sender: TObject);
begin
cdsUfo.Cancel;
end;

Let's write some source code, using the ClientDataSet's events.
- Firstly, we write a procedure that handles the enabling or disabling of certain components, depending from
the fact if we are in the course of modifying a record, or just browsing the table. Without this procedure, we would
have to repeat almost the same code in 4 places.
In the interface section, under Private declarations, add the following line:
procedure UpdateGUI(Modify: Boolean);
- In the implementation section, add the following procedure:
procedure TForm1.UpdateGUI(Modify: Boolean);
begin
DBGrid1.Enabled := not Modify;
DBNavigator1.Enabled := not Modify;
edSearch.Enabled := not Modify;
btnSearch.Enabled := not Modify;
btnWeb.Enabled := not Modify;
panEdit.Visible := Modify;
if Modify then
edID.SetFocus
else
DBGrid1.SetFocus;
end;
- Select dsUfo. In the Object Inspector, tab Events, double click next to BeforeEdit.
Complete the new event handler as follows:
procedure TForm1.cdsUfoBeforeEdit(DataSet: TDataSet);
begin
UpdateGUI(True);
end;
- Likewise, create an event handler for BeforeInsert and complete the code:
procedure TForm1.cdsUfoBeforeInsert(DataSet: TDataSet);
begin
UpdateGUI(True);
end;
- For the event AfterCancel:
procedure TForm1.cdsUfoAfterCancel(DataSet: TDataSet);
begin
UpdateGUI(False);
end;
- For the event AfterPost:
procedure TForm1.cdsUfoAfterPost(DataSet: TDataSet);
begin
DataChanged := True;
UpdateGUI(False);
end;
Database Tutorials - Part 3
Database Tutorials - Part 5
|
|