Skip to main content

Using provider mode: TUserDataSource

  • 4 minutes to read
  1. Base class structure

  2. TPriceList

  3. TUserDataSource

  4. Creating columns

  5. Full code

3. TUserDataSource

The TUserDataSource class overrides several TcxCustomDataSource methods.

A grid column displays data from a specific field of the TListEntry type. To uniquely identify a column in the GetValue and GetInfoForCompare methods, use its DataBinding.Data property. When your column is created (see below), its DataBinding.Data property is initialized with a unique value. The overridden GetItemHandle method returns the DataBinding property value, which is internally passed to the GetValue and GetInfoForCompare methods as an item handle.

GetInfoForCompare retrieves a pointer to the memory address where the required value resides. This function allows you to implement fast sorting operations. Finally, the IsNativeCompare function must be set to True in this instance.

{Constants used for column identification}
const
  DescriptionID = 0;
  OnHandID = 1;
  PriceID = 2;

TUserDataSource implementation:

{ TUserDataSource }
constructor TUserDataSource.Create(APriceList: TPriceList);
begin
  inherited Create;
  FPriceList := APriceList;
end;
{
Returns the TcxGridColumn.DataBinding property for a specified visible index of a column. This property is initialized when a grid column is created.
}
function TUserDataSource.GetDataBinding(AItemIndex: Integer): TcxGridItemDataBinding;
begin
  Result := TcxCustomGridTableItem(DataController.GetItem(AItemIndex)).DataBinding;
end;
{
Returns TcxGridColumn.DataBinding as an item handle for a specified visible index of a column
}
function TUserDataSource.GetItemHandle(AItemIndex: Integer): TcxDataItemHandle;
begin
  Result := TcxDataItemHandle(GetDataBinding(AItemIndex));
end;
{
Returns a pointer to the memory address at which the value of the desired field resides. AItemHandle is an item handle retrieved by the GetItemHandle method.
}
function TUserDataSource.GetInfoForCompare(ARecordHandle: TcxDataRecordHandle; AItemHandle: TcxDataItemHandle; var PValueBuffer: PChar): Boolean;
var
  ADataBinding: TcxGridItemDataBinding;
begin
  ADataBinding := TcxGridItemDataBinding(AItemHandle);
  with PListEntry(FPriceList.FRecords[Integer(ARecordHandle)])^ do
  begin
    case Integer(ADataBinding.Data) of
      DescriptionID:
        PValueBuffer := @Description;
      OnHandID:
        PValueBuffer := @OnHand;
      PriceID:
        PValueBuffer := @Price;
    end;
  end;
  Result := True;
end;
{
Returns the number of records to display in a grid control.
}
function TUserDataSource.GetRecordCount: Integer;
begin
  Result := FPriceList.FRecords.Count;
end;
{
Returns a specific field value from the TListEntry object. ARecordHandle identifies a record in the list. AItemHandle is an item (column) handle retrieved by the GetItemHandle method.
}
function TUserDataSource.GetValue(ARecordHandle: TcxDataRecordHandle; AItemHandle: TcxDataItemHandle): Variant;
var
  ADataBinding: TcxGridItemDataBinding;
  ARecordIndex: Integer;
begin
  ADataBinding := TcxGridItemDataBinding(AItemHandle);
  ARecordIndex := Integer(ARecordHandle);
  with TListEntry(FPriceList.FRecords[ARecordIndex]^) do
  begin
    case Integer(ADataBinding.Data) of
      DescriptionID:
        Result := Description;
      OnHandID:
        Result := OnHand;
      PriceID:
        Result := Price;
    end;
  end;
end;
{
Since the TUserDataSource provides the GetInfoForCompare procedure, the IsNativeCompare function should return True.
}
function TUserDataSource.IsNativeCompare: Boolean;
begin
  Result := True;
end;

The creation of grid columns and the initialization of the TcxGridColumn.DataBinding.Data property is provided in the next topic.

See Also