Back to Devexpress

TdxCustomMemData Class

vcl-dxmdaset-3f6b8b29.md

latest10.5 KB
Original Source

TdxCustomMemData Class

The base class for the TdxMemData component.

Declaration

delphi
TdxCustomMemData = class(
    TDataSet
)

Remarks

The TdxCustomMemData class implements a memory-based dataset component designed as a temporary storage for data-aware controls.

Main API Members

The list below outlines key members of the TdxCustomMemData 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).

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

Field Structure Management

CreateFieldsFromBinaryFile | CreateFieldsFromDataSet | CreateFieldsFromStreamCreates dataset fields from the specified source.CopyFromDataSetCopies the data field structure and data from another data source.

Data Management

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.

Data Shaping

FilterList | ProgrammedFilter | Filtered | OnFilterRecordAllow you to filter data.SortedFields | SortOptionsAllow you to sort data against one or more dataset fields.

Import and Export

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.

Record Navigation

Bof | EofIdentify if the current position in the dataset is at the first or last record.First | Last | Next | PriorNavigate between dataset records.

General-Purpose API Members

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.

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;
#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();
}

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:

delphi
if dxMemData1.Persistent.Option = poActive then
    dxMemData1.Persistent.Option := poNone;
  dxMemData1.Close;
  dxMemData1.Open;
cpp
if(dxMemData1->Persistent->Option == poActive)
    dxMemData1->Persistent->Option = poNone;
  dxMemData1->Close();
  dxMemData1->Open();

Terminal TdxCustomMemData Class Descendants

Do not use the TdxCustomMemData class directly. Use the TdxMemData component instead.

Inheritance

TObject TPersistent TComponent TDataSet TdxCustomMemData

See Also

TdxCustomMemData Members

dxmdaset Unit