Skip to main content

TdxMemData Class

A memory-based dataset component.

Declaration

TdxMemData = class(
    TdxCustomMemData
)

Remarks

The TdxMemData class implements a lightweight memory-based dataset component derived from the TDataSet component shipped with the standard VCL library. TdxMemData is a convenient tool with multiple design-time data management options for test projects with data-aware VCL controls.

Design-Time Functionality

The TdxMemData component ships with design-time dialogs that allow you to create, configure, and populate dataset fields.

Field Editor Dialog

You can use the Field Editor dialog to manage fields in the TdxMemData component. Double-click the component or click Field Editor in its context menu to invoke the corresponding dialog.

VCL Memory Data: The Field Editor Dialog

New Field Dialog

A click on the Add button invokes the New Field dialog that allows you to create a new dataset field from scratch.

VCL Memory Data: The New Field Dialog

To define a new field, you need to specify the following:

  • Field Name
  • Type
  • Size (in characters, applicable to string fields only)
  • Lookup definition (for lookup fields only)

Persistent Editor Dialog

Click Persistent Editor in the component’s context menu to open the corresponding dialog.

VCL Memory Data: The Persistent Editor Dialog

The Persistent Editor dialog allows you to do the following:

  • Add dataset records.
  • Load fields from a text or binary file.
  • Populate loaded fields with data. Use the same file to load both fields and records. Otherwise, the component replaces the previously loaded field structure with fields from the last specified file.
  • Accept all changes applied to your dataset. A click on the OK button also creates a field component (a TField class instance) for each created field.
  • Clear the dataset.

Dataset Copy Functionality

The TdxMemData component’s context menu includes the Assign Data From item if the project includes at least one TDataSet class descendant instance. You can click this menu item to display all available data sources in a nested context menu.

VCL Memory Data: The Dataset Copy Operation

Click an item in the nested context menu to load data from the corresponding source.

Main API Members

The list below outlines key members of the TdxMemData class. These members allow you to populate a memory-based dataset and manage data at runtime.

Data Binding

Active | Close | Open

Allow you to open and close the memory-based dataset. When the dataset is closed, it cannot exchange data with data-aware controls.

Note

You need to close the dataset when you change the field structure or data binding settings (DataSource, for example).

DataSource
Allows you to associate the memory-based dataset with a data source component.

Field Structure Management

CreateFieldsFromBinaryFile | CreateFieldsFromDataSet | CreateFieldsFromStream
Creates dataset fields from the specified source.
CopyFromDataSet
Copies the data field structure and data from another data source.

Data Management

Append | Insert
Create a new empty record.
AppendRecord | InsertRecord
Create a new populated record.
Data
Provides access to the collection of dataset fields stored in memory.
Delete
Deletes the active record and moves the current position to the next record.
GetValueCount
Returns the number of records that contain the specified value.
IsEmpty
Identifies if the dataset contains records.
MoveCurRecordTo
Moves the current record to the specified position in the dataset.
ReadOnly
Allows you to protect data from changes.
RecordCount
Returns the number of stored dataset records.

Data Shaping

FilterList | ProgrammedFilter | Filtered | OnFilterRecord
Allow you to filter data.
SortedFields | SortOptions
Allow you to sort data against one or more dataset fields.

Import and Export

DelimiterChar
Specifies the column delimiter character for text-based files that store exported data.
LoadFromDataSet | LoadFromBinaryFile | LoadFromStream | LoadFromStrings | LoadFromTextFile
Populate the dataset from a source dataset, file, string array, or stream.
SaveToBinaryFile | SaveToStream | SaveToStrings | SaveToTextFile
Save data to a file or stream.

Record Navigation

Bof | Eof
Identify if the current position in the dataset is at the first or last record.
First | Last | Next | Prior
Navigate between dataset records.

General-Purpose API Members

DisableControls | EnableControls
Allow you to avoid excessive redraw operations in associated data-aware controls during batch dataset changes.
Persistent
Provides access to the component’s DFM-related content storage settings.

Code Examples

Create Fields and Records

The following code example creates three dataset fields and two records in a TdxMemData component, and displays data in a bound data-aware grid Table View:

uses
  dxmdaset, cxGrid, cxGridDBTableView;
// ...
procedure TMyForm.CreateField(AMemData: TdxMemData; AFieldName: string; AFieldType: TFieldType);
var
  AFieldDef: TFieldDef;
begin
  if ((AMemData = nil) or (AFieldName = '')) then Exit;
  AFieldDef := AMemData.FieldDefs.AddFieldDef;
  AFieldDef.Name := AFieldName;
  AFieldDef.DataType := AFieldType;
  AFieldDef.CreateField(AMemData);
end;

procedure TMyForm.FormCreate(Sender: TObject);
begin
  dxMemData1.DisableControls;
  try
    if dxMemData1.Active then
      dxMemData1.Close;
    // Create three fields in the memory-based dataset
    CreateField(dxMemData1, 'ID', ftInteger);
    CreateField(dxMemData1, 'FirstName', ftString);
    CreateField(dxMemData1, 'LastName', ftString);
    // Create two records and populate corresponding data cells
    dxMemData1.Open;
    dxMemData1.Append;
    dxMemData1.FieldByName('ID').AsInteger := 0;
    dxMemData1.FieldByName('FirstName').AsString := 'James';
    dxMemData1.FieldByName('LastName').AsString := 'Packard';
    dxMemData1.Append;
    dxMemData1.FieldByName('ID').AsInteger := 1;
    dxMemData1.FieldByName('FirstName').AsString := 'Hannah';
    dxMemData1.FieldByName('LastName').AsString := 'Brooklyn';
    dxMemData1.Post;
  finally
    dxMemData1.EnableControls;
  end;
  cxGrid1DBTableView1.DataController.CreateAllItems();
end;

VCL MemData: A Populated Table Example

Delete All Records

To delete all records, you can reopen the memory-based dataset. Make sure that the dataset’s Persistent.Option property is set to poNone to avoid an automatic data reload operation from a DFM file when the dataset becomes active.

The following code example deletes all records stored in a memory-based dataset:

  if dxMemData1.Persistent.Option = poActive then
    dxMemData1.Persistent.Option := poNone;
  dxMemData1.Close;
  dxMemData1.Open;
See Also