Back to Devexpress

Using provider mode: Base class structure

vcl-166039-expressdatacontroller-provider-mode-using-provider-mode-base-class-structure.md

latest3.7 KB
Original Source

Using provider mode: Base class structure

  • Jul 29, 2021
  • 2 minutes to read

The following topics describe how to implement a custom data source providing price list records to display within a grid:

  1. Base class structure

  2. TPriceList

  3. TUserDataSource

  4. Creating columns

  5. Full code

In the example, the grid control is populated with data from the TPriceList object, which supports the collection of TListEntry elements. TListEntry elements are displayed as records in our grid control. Every grid column presents data from a specific TListEntry field. The TUserDataSource class provides the grid with data from TPriceList.

1. Base class structure

Three main types are defined:

  • TListEntry represents a single record in the list. Every record contains the Description, OnHand, and Price fields, which will be displayed within three grid columns.

  • Delphi

  • C++

delphi
PListEntry = ^TListEntry;
  TListEntry = packed record
    Description: string;
    OnHand: Integer;
    Price: Currency;
  end;
cpp
struct TListEntry {
  String Description;
  int OnHand;
  Currency Price;
};
typedef ::TListEntry* PListEntry;
  • TPriceList is the list of TListEntry records.

  • Delphi

  • C++

delphi
TPriceList = class
  private
    FRecords: TList;
  public
    constructor Create;
    destructor Destroy; override;
    procedure Add(ADescription: string; AOnHand: Integer; 
    APrice: Currency);
    procedure Clear;
  end;
cpp
class TPriceList
{
public:
  TPriceList();
  ~TPriceList();
  void Add(String ADescription, int AOnHand, Currency APrice);
  void Clear();
  TList *FRecords;
};
delphi
TUserDataSource = class(TcxCustomDataSource)
  private
    FPriceList: TPriceList;
    function GetDataBinding(AItemIndex: Integer):
    TcxGridItemDataBinding;
  protected
    function GetInfoForCompare(ARecordHandle: TcxDataRecordHandle;
    AItemHandle: TcxDataItemHandle; var PValueBuffer: PChar):
    Boolean; override;
    function GetItemHandle(AItemIndex: Integer): TcxDataItemHandle;
    override;
    function GetRecordCount: Integer; override;
    function GetValue(ARecordHandle: TcxDataRecordHandle;AItemHandle:
    TcxDataItemHandle): Variant; override;
    function IsNativeCompare: Boolean; override;
  public
    constructor Create(APriceList: TPriceList);
  end;
cpp
class TUserDataSource : public TcxCustomDataSource
{
private:
 TPriceList *FPriceList;
 TcxGridItemDataBinding* GetDataBinding(int AItemIndex);
protected:
  bool __fastcall GetInfoForCompare(void * ARecordHandle, 
  void * AItemHandle, char * &PValueBuffer);
  void* __fastcall GetItemHandle(int AItemIndex);
  int __fastcall GetRecordCount();
  Variant __fastcall GetValue(void * ARecordHandle, 
  void * AItemHandle);
  bool __fastcall IsNativeCompare();
public:
  TUserDataSource(TPriceList *APriceList);
};

In the next section the TPriceList class implementation is outlined.

See Also

TcxCustomDataSource