简体   繁体   中英

C++ builder binding data at runtime

Trying to bind TClientDataSet to TStringGrid at runtime on Delphi modele below:

type
  TForm2 = class(TForm)
    ClientDataSet1: TClientDataSet;
    Grid1: TGrid;
    StringGrid1: TStringGrid;
    procedure FormCreate(Sender: TObject);
  private
  public
  end;
[...]

procedure TForm2.FormCreate(Sender: TObject);
var
  AField : TField;
  BindSourceDB1 : TBindSourceDB;
  LinkGridToDataSourceBindSourceDB1 : TLinkGridToDataSource;
  LinkGridToDataSourceBindSourceDB2 : TLinkGridToDataSource;
begin
  AField := TIntegerField.Create(Self);
  AField.FieldName := 'ID';
  AField.FieldKind := fkData;
  AField.DataSet := ClientDataSet1;

  AField := TStringField.Create(Self);
  AField.FieldName := 'Name';
  AField.Size := 20;
  AField.FieldKind := fkData;
  AField.DataSet := ClientDataSet1;

  BindSourceDB1 := TBindSourceDB.Create(Self);
  BindSourceDB1.DataSet := ClientDataSet1;

  LinkGridToDataSourceBindSourceDB1 := TLinkGridToDataSource.Create(Self);
  LinkGridToDataSourceBindSourceDB1.DataSource := BindSourceDB1;
  LinkGridToDataSourceBindSourceDB1.GridControl := Grid1;

  LinkGridToDataSourceBindSourceDB2 := TLinkGridToDataSource.Create(Self);
  LinkGridToDataSourceBindSourceDB2.DataSource := BindSourceDB1;
  LinkGridToDataSourceBindSourceDB2.GridControl := StringGrid1;

  ClientDataSet1.IndexFieldNames := 'ID';
  ClientDataSet1.CreateDataSet;
  ClientDataSet1.InsertRecord([1, 'AName']);
  ClientDataSet1.InsertRecord([2, 'AnotherName']);
  ClientDataSet1.InsertRecord([3, 'ThirdName']);
  ClientDataSet1.InsertRecord([4, 'FourthName']);
  ClientDataSet1.InsertRecord([5, 'FifthName']);
  ClientDataSet1.First;

end;

Unfortunately, I didn't manage to instantiate the LinkGridToDataSource object. I received following error message:

Allocating an object of abstract class type 'Data::Bind::Grid::TLinkGridToDataSource'.

Here is my code:

  ...
  BindSourceDB1 = new TBindSourceDB(this);
  BindingsList1 = new TBindingsList(this);
  LinkGridToDataSource1 = new TLinkGridToDataSource(this);
  ...

Any help?

Finally the code below worked:

in unit.h

...
#include <REST.Client.hpp>  // For TRESTClient, TRESTRequest & TRESTResponse
#include <REST.Response.Adapter.hpp>  // For TRESTResponseDataSetAdapter
#include <Datasnap.DBClient.hpp>   // For TClientDataSet
#include <Data.Bind.DBScope.hpp>   // For TBindSourceDB
#include <Data.Bind.Grid.hpp>  // For TLinkGridToDataSource
...
class oTLinkGridToDataSource : public TLinkGridToDataSource
{protected:
  virtual void __fastcall Reactivate() override;
  virtual bool __fastcall RequiresControlHandler() override;

 public:
   __fastcall virtual oTLinkGridToDataSource(System::Classes::TComponent* AOwner){}
   __fastcall virtual ~oTLinkGridToDataSource(){}
};
...

in unit.cpp

void __fastcall oTLinkGridToDataSource::Reactivate()
{//
}
//-----
bool __fastcall oTLinkGridToDataSource::RequiresControlHandler()
{return true;
}
//-----
void __fastcall TForm1::FormShow(TObject *Sender)
{
 TRESTClient *RESTClient1 = new TRESTClient(this);
 TRESTRequest *RESTRequest1 = new TRESTRequest(this);
 TRESTResponse *RESTResponse1 = new TRESTResponse(this);
 TRESTResponseDataSetAdapter *RESTResponseDataSetAdapter1 = new TRESTResponseDataSetAdapter(this);
 TClientDataSet *ClientDataSet1 = new TClientDataSet(this);

 RESTClient1->BaseURL = "myURL.com";
 RESTRequest1->Client = RESTClient1;
 RESTRequest1->Response = RESTResponse1;

 RESTResponse1->ContentType = "application/json";
 RESTResponseDataSetAdapter1->Dataset = ClientDataSet1;
 RESTResponseDataSetAdapter1->Response = RESTResponse1;
 RESTResponseDataSetAdapter1->Active = true;
 RESTRequest1->Execute();

TBindSourceDB *BindSourceDB1 = new TBindSourceDB(this);
oTLinkGridToDataSource *LinkGridToDataSource1 = new oTLinkGridToDataSource(this);
 //--
 BindSourceDB1->DataSet = ClientDataSet1;
 LinkGridToDataSource1->BindingsList = BindingsList1;
 LinkGridToDataSource1->DataSource = BindSourceDB1;
 LinkGridToDataSource1->GridControl = StringGrid1;

}
//-----

Hope this will help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM