NextGrid & NextDBGrid








NextGrid .NET

NxVirtualDataSet tutorial

Related Articles:

  • Skill:
  • Version: 1.0.0
  • Author: Bojan Nikolic
  • Created: 2008-11-21
  • Updated: 2009-02-28
The TNxVirtualDataSet is a virtualized descendant of TDataSet. This implies that information about data it supplies and the data itself must be suplied just in time, i.e. at the moment Db components using the TNxVirtualDataSetneed it.

Because of this virtualization it is possible to use the TNxVirtualDatSet to turn almost anything rectangular (like a TCollection or a TStringList) into a DataSet. It is also possible to turn hardware into a database (like values from an usb thermometer or a joystick). Computed fields are also easy to implement (like the length of a line in a TStringList).

Virtualized are the following parts of the TDataSet:

Part Event
Number of fields OnFieldCount event
Field definitions OnAddField event
Number of records OnRecordCount event
Reading Field values OnGetFieldData / OnGetBlobData events
Writing Field values OnSetFieldData / OnSetFieldMode events

Request for data for these virtualized parts are peformed through a number of events.

Normally the first event handler called is the number of fields, after which TNxVirtualDataSet needs the definition of each field.

Shortly afterwards TnxVirtualDataSet will need the number of records. Then TnxVirtualDataSet starts making numerous requests for field values at a high pace. Efficient coding of this event is required as it is used when visual Db Controls are repainted.

Write support is experimental and a bit tricky. After an Append, Edit or Insert call to TNxVirtualDataSet the OnSetFieldData event is used to signal modification to field data. The program must cache this field data as it is not clear at that moment what operation will be performed on which record.

When the programm calls the Post, Cancel or Delete method, the OnSetFieldMode event is called with a set of UpdateModes. Normally it is a combination of umAppend, umEdit or umInsert and umPost. Depending on the presence of umAppend, umEdit or umInsert the programmer must add, change or insert a new record in the underlying storage. As parameters of the OnSetFieldMode event, TnxVirtualDataSet passes the recordnumber of the involved record.

Not combined with other values are the UpdateModes umDelete and umCancel. On encountering umDelete, the program must delete the involved. The last mode, umCancel simply means ignoring the previously changed data.

The demo application uses a TStringList to store lines of text. Each line represents a single record. The TNextDbGrid displays the followind data:

Field 0 the index of the line

Field 1 the text of the line

Field 2 the length of the line

Figure 1. Appending a record to the TNxVirtualDataSet.

function TForm1.NxVirtualDataSet1GetFieldData(Sender: TObject;
  const rowIndex, fieldIndex: Integer): OLEVariant;
  case fieldIndex of
    0: Result := NxVirtualDataSet1.RecNo;
    1: if Assigned(fStorage) and (rowIndex >= 0) and (rowIndex < fStorage.Count)
         Result := fStorage[rowIndex]
         Result := '**Error**';
    2: if Assigned(fStorage) and (rowIndex >= 0) and (rowIndex < fStorage.Count)
         Result := Length(fStorage[rowIndex])
         Result := '-1';

Was This Article Useful?

Only constructive comments, code contributions... will be publishes. Questions, non-official discussion will not be published.