上一篇咱们演示了如何绑定一个对象,这是十分有用的, 不爽的的是在设计期间不能晓得对象有几个属性(FisrtName, LastName, Age在设计期间都看不见),
为了解决这个问题咱们应用TDataGeneratorAdapter控件
一、拖入一个TDataGeneratorAdapter控件并点击Add Field
二、设计实现后连贯StringGrid与TDataGeneratorAdapter绑定
到些设计曾经实现,但咱们依然配置OnCreateAdapter事件 咱们在DataGeneratorAdapter创立的字段匹配TPerson类 如果咱们运行程序会通过 DataGeneratorAdapter通过TObjectBindSourceAdapter 替换成 AdapterBindSource,
如果咱们不须要这个事件, AdapterBindSource会应用DataGeneratorAdapter也能够运行
NOTE:
1.DataGeneratorAdapter相似DataSet 能够编辑、提交、保留
2.当须要一个DataSet时能够应用DataGeneratorAdapter
; "复制代码")
MyPeople : TObjectList<TPerson>; procedure TForm1.AdapterBindSource1CreateAdapter(Sender: TObject; var ABindSourceAdapter: TBindSourceAdapter); begin MyPeople := TObjectList<TPerson>.Create();
MyPeople.Add(TPerson.Create('Fred', 'Flintstone', 40));MyPeople.Add(TPerson.Create('Wilma', 'Flintstone', 41));MyPeople.Add(TPerson.Create('Barney', 'Rubble', 40));MyPeople.Add(TPerson.Create('Betty', 'Rubble', 39));ABindSourceAdapter := TListBindSourceAdapter<TPerson>.Create(Self, MyPeople, True); end;
; "复制代码")