Using MemDataSets in Lazarus
- populate a dataset with types such as string, integer and real using Pascal code;
- save the data to a local file;
- load the data from the local file to the dataset;
- use buttons on the DBNavigator component to step through the records;
- edit data directly in the DBGrid and insert and populate new records in the grid;
- make use of the inbuilt validation whereby, for example, digits only are allowed in an integer field.
Most of the steps make use of the Object Inspector.
- Start a new application and select menu item .
- Save the project as MemDatasetDemo1 and the unit as uMemDatasetDemo1.
- Drop onto the form a TMemDataset and TDataSource (Data Access tab) and a TDBNavigator and TDBGrid (Data Controls tab).
In the Object Inspector, right-click on MemDataset1 and select menu item Create dataset.
This step does not provide you with the opportunity to enter some data as it does in Delphi's TClientDateset and it might not be necessary in Lazarus, but it does no harm.
- Click in the FieldDefs edit box then on the ellipsis icon to bring up the FieldDefs editor.
- Click on the + icon to add a FieldDef and change its data type to ftString, its name to Strings and its size to 10.
- Similarly create a FieldDef named Integers with type ftInteger, Size 4 and Precision 4.
- Similarly create a FieldDef named Floats with type ftFloat, Size 8 and Precision 4.
In the Object Inspector, select MemDataset1 for the Dataset property of DataSource1.
MemDataset1 is, of course, the only dataset in the drop-down list.
- Similarly, set the DataSource property of DBGrid1 to DataSource1.
- Right-click on DBGrid1 in the Object Inspector and select the menu item Edit Columns ... .
- Click the + icon three times to add three columns.
Select the FieldName property of the first column to be Strings, the second to be Integers and the third to be Floats.
The column titles will given the same name as the field name, but you can change them if you wish.
- Set the width of DBGrid1 to be 300 and suitably position it and the other visible component, DBNavigator1.
- Select the DataSource property of DBNavigator1 to be DataSource1.
- Double click on the form to create the FormCreate procedure and paste the following code into it between begin and end.
with MemDataSet1 do begin if fileExists('demo.mds') then begin LoadFromFile('demo.mds') ; Active := true; end else begin Active := true; Append; Fields.Value := 'One'; Fields.Value := 1; Fields.Value := 1.2; Append; Fields.Value := 'Two'; Fields.Value := 2; Fields.Value := 2.3; Append; Fields.Value := 'Three'; Fields.Value := 3; Fields.Value := 3.4; Post; SaveToFile('demo.mds'); end; end;
Double click in the OnClose event edit box to create the FormClose procedure and paste this line of code into it between begin and end:
Execute the program, confirm that you can navigate through the records, and click on the + icon to add a new row:
Inserting a row
Type in the data Four, 4 and 4.5 then click in another row or on the Post (tick) icon to store the changes. Close the application then execute it again.
You should find that your new data has been saved to file and reloaded successfully.
- Try editing data and also typing erroneous data into the number columns or a string longer than ten characters into the first column.
The second demo (on the next page) requires the same kind of set-up and has a labelled edit box to contain search data and a button to execute the search. It transfers the initial (real world) data from arrays to the dataset.