vcl-dxmdaset.md
A memory-based dataset component.
TdxMemData = class(
TdxCustomMemData
)
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.
The TdxMemData component ships with design-time dialogs that allow you to create, configure, and populate dataset fields.
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.
A click on the Add button invokes the New Field dialog that allows you to create a new dataset field from scratch.
To define a new field, you need to specify the following:
Click Persistent Editor in the component’s context menu to open the corresponding dialog.
The Persistent Editor dialog allows you to do the following:
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.
Click an item in the nested context menu to load data from the corresponding source.
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.
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).
DataSourceAllows you to associate the memory-based dataset with a data source component.
CreateFieldsFromBinaryFile | CreateFieldsFromDataSet | CreateFieldsFromStreamCreates dataset fields from the specified source.CopyFromDataSetCopies the data field structure and data from another data source.
Append | InsertCreate a new empty record.AppendRecord | InsertRecordCreate a new populated record.DataProvides access to the collection of dataset fields stored in memory.DeleteDeletes the active record and moves the current position to the next record.GetValueCountReturns the number of records that contain the specified value.IsEmptyIdentifies if the dataset contains records.MoveCurRecordToMoves the current record to the specified position in the dataset.ReadOnlyAllows you to protect data from changes.RecordCountReturns the number of stored dataset records.
FilterList | ProgrammedFilter | Filtered | OnFilterRecordAllow you to filter data.SortedFields | SortOptionsAllow you to sort data against one or more dataset fields.
DelimiterCharSpecifies the column delimiter character for text-based files that store exported data.LoadFromDataSet | LoadFromBinaryFile | LoadFromStream | LoadFromStrings | LoadFromTextFilePopulate the dataset from a source dataset, file, string array, or stream.SaveToBinaryFile | SaveToStream | SaveToStrings | SaveToTextFileSave data to a file or stream.
Bof | EofIdentify if the current position in the dataset is at the first or last record.First | Last | Next | PriorNavigate between dataset records.
DisableControls | EnableControlsAllow you to avoid excessive redraw operations in associated data-aware controls during batch dataset changes.PersistentProvides access to the component’s DFM-related content storage settings.
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;
#pragma link "dxmdaset"
#pragma link "cxGrid"
#pragma link "cxGridDBTableView"
// ...
void __fastcall TMyForm::CreateField(TdxMemData *AMemData, UnicodeString AFieldName, TFieldType AFieldType)
{
if((AMemData == nullptr) || (AFieldName == "")) { return; }
TFieldDef *AFieldDef = AMemData->FieldDefs->AddFieldDef();
AFieldDef->Name = AFieldName;
AFieldDef->DataType = AFieldType;
AFieldDef->CreateField(AMemData);
}
void __fastcall TMyForm::FormCreate(TObject *Sender)
{
dxMemData1->DisableControls();
try
{
if(dxMemData1->Active)
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();
}
cxGrid1DBTableView1->DataController->CreateAllItems();
}
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;
if(dxMemData1->Persistent->Option == poActive)
dxMemData1->Persistent->Option = poNone;
dxMemData1->Close();
dxMemData1->Open();
TObject TPersistent TComponent TDataSet TdxCustomMemData TdxMemData
See Also
VCL Charts: Line View Tutorial. Step 2. Bind Series to Data
VCL Data Grid: Table View Tutorial. Step 1. Bind to a Data Source