Skip to content

TTMSFNCDataSet

The TTMSFNCDataSet is an abstract dataset implementation that allows loading data from various data structures.

TTMSFNCDataSet allows dynamic data linking through the DataObject/DataLink properties. If DataObject/DataLink is assigned and the assigned object implements the ITMSFNCDataObject interface, TTMSFNCDataSet can retrieve data from that object, regardless of its underlying structure. This flexibility allows the dataset to interact with a wide variety of data structures, such as TList, TDictionary, TStringList, or even other controls.

Filtering

In TTMSFNCDataSet, filtering can be achieved in two ways: by using the Filter property or by implementing the OnFilterRecord event. Both methods allow you to define which records are visible in the dataset based on specified criteria.

Filtering with the Filter property

The Filter property allows you to define filtering rules using a custom filter string. This mechanism has been implemented from scratch in TTMSFNCDataSet, and while it supports similar functionality to the standard TDataset filter, there are some differences. For example, in TTMSFNCDataSet, logical operators such as | (for "OR") and & (for "AND") are used to filter data.

Example

procedure TForm1.Button1Click(Sender: TObject);
begin
  // Disable the filter
  TMSFNCDataSet1.Filtered := False;

  // Filter records where the 'Country' field is either 'Belgium' or 'Germany'
  TMSFNCDataSet1.Filter := 'Country = "Belgium" | Country = "Germany"';

  // Alternatively, using QuotedStr to safely quote string values
  TMSFNCDataSet1.Filter := 'Country = ' + QuotedStr('Belgium') + ' | Country = ' + QuotedStr('Germany');

  // Enable the filter
  TMSFNCDataSet1.Filtered := True;
end;

Filtering with the OnFilterRecord event

The OnFilterRecord event allows to define custom filtering logic for each record. When Filtered is set to True, only the records that meet the criteria in the OnFilterRecord event will be visible in the dataset.

Example

procedure TForm1.TMSFNCDataSet1FilterRecord(DataSet: TDataSet; var Accept: Boolean);
begin
  // Example: Only accept records where the 'Name' field contains 'John'
  Accept := DataSet.FieldByName('Name').AsString.Contains('John');
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  TMSFNCDataSet1.Filtered := False;
  TMSFNCDataSet1.OnFilterRecord := TMSFNCDataSet1FilterRecord;
  TMSFNCDataSet1.Filtered := True;
end;

Sorting

To sort records in the TTMSFNCDataSet, use the SortBy method to sort by a specific field and the Unsort method to revert to the original order. The SortBy method accepts a field name as a parameter and automatically sorts the dataset based on that field. You can easily toggle between sorted and unsorted states by using these methods.

Example

// Sort the dataset by the 'Age' field
TMSFNCDataSet1.SortBy('Age');

// To revert the dataset to its original unsorted order
TMSFNCDataSet1.Unsort;

Linking to data structures

The TTMSFNCDataSet provides two properties for linking to custom data structures:

  • DataLink: Use this when linking to a custom data structure at design-time (= to a TComponent descendant). The data structure is expected to implement the ITMSFNCDataObject interface.
  • DataObject: Use this when linking to a data-structure at run-time. The data structure is expected to implement the ITMSFNCDataObject interface.

TTMSFNCDataLinkCSV and TTMSFNCDataLinkJSON components can be used to load CSV and JSON data.

For custom datastructures, the implementation of ITMSFNCDataObject is required.

TTMSFNCDataSet

Hint

For an implementation of ITMSFNCDataObject, check out our TTMSFNCDataSet/CustomDataLink demo.

ITMSFNCDataObject interface

The ITMSFNCDataObject interface defines a flexible contract for data objects used within the TTMSFNCDataSet, enabling interaction with various data structures. Classes implementing this interface are required to provide key methods for dataset management.

Method name Description
Activate(AValue: Boolean) Signals whenever the TTMSFNCDataSet Active property is set.
AddRecord(ARecord: TValue) Adds a new record to the data source, with the value specified by ARecord.
CompareRecords(AField: string; ARecord1, ARecord2: TValue): Integer Compares the values of the field AField between two records, ARecord1 and ARecord2. Returns a comparison result used for sorting.
CreateDefaultRecord: TValue Creates and returns a default record for the data source. This should be a default empty record, not a nil value.
DeleteRecord(ARecord: Integer) Deletes the record at the specified AIndex.
GetRecord(AIndex: Integer): TValue Returns the record at the specified index AIndex.
GetRecordCount: Integer Returns the total number of records in the data source.
GetRecordIndex(ARecord: TValue): Integer Finds the index of a record that matches the specified value ARecord.
GetRecordValue(ARecord: TValue; AField: string; AFieldType: TFieldType): ValueType Retrieves the value of the field AField from the specified record ARecord.
InitializeFieldDefs(AFieldDefs: TFieldDefs) Initializes field definitions for the dataset whenever no Fields are added.
InsertRecord(AIndex: Integer; ARecord: TValue) Inserts a new record at the specified AIndex with the value specified by ARecord.
SetRecordValue(ARecordIndex: Integer; AField: string; AValue: ValueType) Sets the value of the field AField in the record at index ARecordIndex to AValue.
SetNotification(ANotifyObject: ITMSFNCDataObjectNotification) Used to establish a communication link between the TTMSFNCDataSet and the data object implementing ITMSFNCDataObject.
procedure TMyDataLink.SetNotification(
ANotifyObject: ITMSFNCDataObjectNotification);
begin
//Store the reference to the dataset notification object
FNotifyObject := ANotifyObject;
end;

procedure TMyDataLink.DeactivateDataset;
begin
FNotifyObject.Deactivate;
end;

ITMSFNCDataLinkNotification interface

The ITMSFNCDataObjectNotification interface is used by TTMSFNCDataSet to manage notification events. See SetNotification for more detail.

Method name Description
DataRefresh Sends a signal for dataset refreshing.
Deactivate Sends a signal for dataset deactivation.