Using BufDataSets in Lazarus
We have experimented with the TBufDataset component in Lazarus and found it (like the less-functional TMemdataset) trickier to use and less reliable than the TClientDataset component in Lazarus. This Free Pascal wiki page was helpful, but more complicated than necessary to enable a school student to get started.
- 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;
- sort the data by any of the fields;
- filter the records so that only those matching your criteria are shown in the grid.
Most of the steps make use of the Object Inspector.
- Start a new application and select menu item .
- Save the project as BufDatasetDemo1 and the unit as uBufDatasetDemo1.
- Drop onto the form a TBufDataset and TDataSource (Data Access tab), a TDBNavigator and TDBGrid (Data Controls tab) and a CheckBox (Standard tab).
- In the Object Inspector, set the Filtered property of BufDataset1 to true and enter Strings for its IndexFieldNames property.
- In the Object Inspector click on the triangle to the left of BufDataset1, right click on <unknown collection>: TFieldDefs and select the menu item Add Item.
- Change the data type of the new FieldDef 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 BufDataset1 for the Dataset property of DataSource1.
BufDataset1 is, of course, the only dataset in the drop-down list.
- Similarly, set the DataSource property of DBGrid1 and DBNavigator1 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 Checked property of CheckBox1 to be true and change its Caption property to Filtered.
- Set the width of DBGrid1 to be 300 and suitably position it and the other visible components, DBNavigator1 and CheckBox1. Adjust the size of the form.
- Double click on the form to create the FormCreate procedure and paste the following code into it between begin and end.
if FileExists('demo1.bds') then BufDataSet1.LoadFromFile('demo1.bds') else with BufDataSet1 do begin CreateDataSet; 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; Append; Fields.Value := 'Four'; Fields.Value := 4; Fields.Value := 4.5; Post; SaveToFile('demo1.bds'); end;
Note that we include the instruction CreateDataset and do not change the value of the Active property of the dataset to true (as we did with the MemDataset in the previous section).
Double click in the OnClose event edit box of the form to create the FormClose procedure and paste these lines of code into it between begin and end:
BufDataSet1.Filtered := False; BufDataSet1.SaveToFile('demo1.bds');
- Double click in the OnFilterRecord property of BufDataset1 and paste this line of code between begin and end of the newly created procedure:
Accept := BufDataSet1.Fields.AsString = 'T';
Double click in the OnChange event of CheckBox1 and paste this line of code between begin and end of the newly created procedure:
BufDataSet1.Filtered := Checkbox1.Checked;
Execute the program:
Filtered sorted data
As expected, only the strings beginning with T are shown and they are in alphabetical order.
- Uncheck the CheckBox to view all of the records. Confirm that you can navigate through the records, and click on the + icon to add a new row.
Type in the data Five, 5 and 5.6 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 ComboBox to choose the Sort field, a label, two labelled edit boxes to enter a range for the filter and a button to execute the filter. It transfers the initial (real world) data from arrays to the dataset.