Skip to main content

TdxMemData - Getting Started

  • 3 minutes to read

Assume we need to create an application. In one of its modules a user can edit (edit, delete, add) the names of Internet friends.

Data Specifications:

Object countries (Id, country name) and object Internet friends (Id, first name, last name, nickname, e-mail, birthday, interests and country).

We could use standard BDE tables, but for the sake of this example, assume we don’t want to use any database files, and of course, we don’t want to distribute our application with powerful, but very large DLLs.

In this instance, we have two options:

The first solution is a good one if you have lots of time or are not concerned with data validation, such as required values or correct values. Obviously, you have to do an incredible amount of work to implement controls such as lookups, etc.

Let’s now show you how easy it is to use our TdxMemData to resolve just this type of a problem.

  1. Drop two TdxMemData components onto a form. Name the first mdCountries, the second mdIntFriends.

  2. Double click on the mdCountries component to call the MemData Field Editor window and press the Add… button. The New Field form should now be active:

To add a new field you must fill:

  • Field Name;

  • Type;

  • Size (for string fields only);

  • Field Type (Default data);

  • Lookup definition for lookup fields only.

  1. Create two fields: ID (Integer) and Name (String, char(50)), and close the MemData Field Editor window.

  2. Double click on the mdIntFriends component and add its fields:

ID (Integer), FirstName (String char(25)), LastName (String char(25)), NickName (String char(10)), Email (String char(80)), birthday ((Date), interests (String char(250)) and CountryID (Integer).

  1. Now we have to add the lookup field to bind mdIntFreinds.CountryID to mdCountries.ID.

Name it CountryName, type ftString, Size 50, field type – lookup and lookup definition:

Key Field – CountryID, DataSet – mdCountries, Lookup Field – ID and Result Field – Name.

Now we have a lookup field. In both the Borland DBGrid and ExpressQuantumGrid™ by Developer Express Inc., this will be represented as a lookup combo (in the ExpressQuantumGrid, it is represented as an incremental lookup combo).

  1. Now we have to set correct ID field values for our TdxMemData components. Write the following event handler for onAfterInsert:
procedure TForm1.dmCountriesAfterInsert(DataSet: TDataSet);
begin
  DataSet.FindField('ID').AsInteger := DataSet.FindField('recid').AsInteger;
end;

You can use this code for both components: dmCountries and dmIntFriends. ‘RECID’ is the field that is created for each TdxMemData. You can see it at design time in the Object Inspector or TdxMemData fields editor.

It is a unique, Auto Incremental field. In this demo, we use it as an index field to get the next value for our ID fields. It can be used as a KeyField in the dxDBGrid control.

  1. Now we have to write code to save and restore data in a text file. Here it is:
// Loads the data on the form OnCreate event, checks if the file exists
procedure TForm1.FormCreate(Sender: TObject);
var
   FileName : string;
begin
  FileName := ExtractFileDir(Application.ExeName) + '\countries.txt';
  if  FileExists(FileName) then
     mdCountries.LoadFromTextFile(FileName);
  FileName := ExtractFileDir(Application.ExeName) + '\intfriends.txt';
  if  FileExists(FileName) then
     mdIntFriends.LoadFromTextFile(FileName);
end;
// Saves the data on the form destroy event
procedure TForm1.FormDestroy(Sender: TObject);
begin
  mdCountries.SaveToTextFile(ExtractFileDir(Application.ExeName) + '\countries.txt');
mdIntFriends.SaveToTextFile(ExtractFileDir(Application.ExeName) + '\intfriends.txt');
end;
  1. Now drop two TDataSources and name them dsCountries and dsIntFriends, then link them with your TdxMemData components.

  2. Set Active to True for both TdxMemData components. The final step is to link your data sources with individual data controls.

And that’s all you need to do!

See Also