How to use a TcxCustomDataSource in a TcxExtLookupComboBox?

白昼怎懂夜的黑 提交于 2019-12-23 18:15:47

问题


I use a TcxExtLookupComboBox from Devexpress and try to implement a custom datasource. I have set the customdatasource like this:

procedure TMainForm.FormCreate(Sender: TObject);
begin
  fDataSource := TMyDataSource.Create;
  cbotestSearch.Properties.View.DataController.CustomDataSource := fDataSource;
end;

TMyDataSource is defined here:

unit Datasource;

interface

uses
  Classes,
  IBQuery,
  SysUtils,
  cxCustomData;

type
  TSearchItem = class
    private
      BoldID: String;
      Display: String
  end;

  TMyDataSource = class(TcxCustomDataSource)
  private
    fSearchList: TList;
  protected
    function GetRecordCount: Integer; override;
    function GetValue(ARecordHandle: TcxDataRecordHandle; AItemHandle: TcxDataItemHandle): Variant; override;
  public
    constructor Create;
    destructor Destroy; override;
    procedure GetData;
  end;

implementation

constructor TMyDataSource.Create;
begin
  inherited Create;
  fSearchList := TList.Create;
end;

destructor TMyDataSource.Destroy;
begin
  FreeAndNil(fSearchList);
  inherited;
end;

procedure TMyDataSource.GetData;
var
  vItem: TSearchItem;
begin
  fSearchList.Clear;

  vItem := TSearchItem.Create;
  vItem.BoldID  := '1000';
  vItem.Display := 'test';
  fSearchList.Add(vItem);

  vItem := TSearchItem.Create;
  vItem.BoldID  := '1100';
  vItem.Display := 'test2';
  fSearchList.Add(vItem);

  DataChanged;    // Don't do anything as provider is nil
end;

function TMyDataSource.GetRecordCount: Integer;
begin
  // Is never entered
  Result := fSearchList.Count;
end;

function TMyDataSource.GetValue(ARecordHandle: TcxDataRecordHandle;
  AItemHandle: TcxDataItemHandle): Variant;
begin
  // Is never entered
  Result := 'Test';
end;

end.

The problem is that TMyDataSource.GetValue is never called. Any hint how to fix ?

Update 1: I have another hint here. If I single step in the DataChanged method that should cause GetValue to be called is looks like this:

procedure TcxCustomDataSource.DataChanged;
begin
  if Provider = nil then Exit;

  // Code using Provider
end;

and Provider is nil in this case. But I have assigned the Datasource in Forms oncreate as you see.


回答1:


cxExtLookupComboBox can only work with DB~views. Such views cannot accept instances of the TcxCustomDataSource object as a DataSource. So, your code will not work :-(. There is a suggestion to implement this feature in the future and it is registered at:

http://www.devexpress.com/Support/Center/ViewIssue.aspx?issueid=AS10025



来源:https://stackoverflow.com/questions/3395739/how-to-use-a-tcxcustomdatasource-in-a-tcxextlookupcombobox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!