How popular is a Grid with a list of users using Livebindings?

Asked

Viewed 740 times

8

How to popular a Grid with a list of users using Livebindings?

Having created a type:

TPessoa = class
private
  FId: integer;
  FNome: string;
  FDataNascimento: TDate;
public
  property Id: integer read FId write FId;
  property Nome: string read FNome write FNome;
  property DataNascimento: TDate read FDataNascimento write FDataNascimento;
end;

I added some components to the form:

  1. Tstringgrid
  2. Tdatageneratoradapter
  3. Tadapterbindsource
  4. Tbindnavigator

LiveBindings_form_and_components

BindingList was automatically created.

To feed Adapterbindsource I created a variable of type Tobjectbindsourceadapter in my form:

TMainForm = class(TForm)
  strgList: TStringGrid;
  DataGeneratorAdapter: TDataGeneratorAdapter;
  AdapterBindSource: TAdapterBindSource;
  bindNav: TBindNavigator;
  BindingsList: TBindingsList;
  LinkGridToDataSourceAdapterBindSource: TLinkGridToDataSource;
  procedure FormCreate(Sender: TObject);
  procedure FormClose(Sender: TObject; var Action: TCloseAction);
  procedure AdapterBindSourceCreateAdapter(Sender: TObject; 
    var ABindSourceAdapter: TBindSourceAdapter);
private
  FPessoas: TObjectBindSourceAdapter<TPessoa>;
end;

...

procedure TMainForm.AdapterBindSourceCreateAdapter(Sender: TObject; 
  var ABindSourceAdapter: TBindSourceAdapter);
begin
  ABindSourceAdapter := FPessoas;
end;

My difficulty is in popular this list.
That’s why I thought:

procedure TMainForm.FormCreate(Sender: TObject);
var
  index: integer;
  pessoa: TPessoa;
begin
  FPessoas := TObjectBindSourceAdapter<TPessoa>.Create(self);
  for index := 1 to 100 do
  begin
    FPessoas.Append;

    pessoa := TPessoa.Create;
    pessoa.Id := index;
    pessoa.Nome := 'Nome ' + index.ToString();
    pessoa.DataNascimento := StrToDate('01/01/2000') + index;

    FPessoas.SetDataObject(pessoa);
  end;
end;

Well, that doesn’t work!
How to do?
There are other ways to do this with Livebindings?

Delphi in version XE7.

  • http://answall.com/questions/75037/como-fazer-livebindings-com-objetos-e-pegar-os-objetos-contidos

1 answer

1

To popular the Grid with a list of objects you must use Tlistbindsourceadapter and not Tobjectbindsourceadapter.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.