Delphi XE2 BindingList与list 比较

        BindingList的主要特点在于,它可以在加入新的元素或者元素发生修改的时候触发相应的事件,而在基本集合类中是不具有这些功能的,而且由于在基本集合中Add方法不是Virtual方法,要想让它触发时间还真不是很方便。不过BindingList可以很好的满足你的功能。

    在Windows表单编程中,使用DataGridView可以很方便与数据库查询结果绑定,但是有时候查出来的数据需要经过一番处理才显示,这时候就不能直接绑定了。借助于BindingList,你便可以很方便得实现这个功能。
     BindingList是一个泛型容器,可以直接赋值给DataGridView的DataSource。而且以后的数据变化,只需要维护BindingList里的东西就行了,DataGridView自动更新数据显示。

最近做项目中突然发现还有个BindingList可以使用。查了不少信息。得出底下几点结论:

  1. 压根不是同一样东西,从命名空间中就能发现,一个是Component下的东西。主要特点在于Component的相关。另一个是Collection命名空间下的产物。主要用于集合相关。
  2. BindingList比List多的功能问为但他是GridView的数据源时,他有Allow....  Sort...等方法。从名字中可以看出对于该数据的一些约束。List比BindingList多的方法比如,AddRange方法,我之前项目中使用BindingList时需要将2个BindingList合并,却找不到方法。当然他可能有其他的方法而我不知道而已。
  3. 最后,BindingList和List并无任何关系不存在继承关系。个人建议,处理逻辑时用List,但作为数据源绑定是用BindingList.
unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Data.Bind.EngExt, Vcl.Bind.DBEngExt,
  Vcl.StdCtrls, Data.Bind.Components, Vcl.Grids;

type
  TPerson = class(TObject)
  protected
    fName: string;
    fAge: integer;
  procedure
    SetName(const Value: string);
  public
    property Name: string read fName write SetName;
    property Age: integer read fAge write fAge;
  end;

type
  TForm1 = class(TForm)
    BindScope1: TBindScope;
    BindingsList1: TBindingsList;
    Edit1: TEdit;
    btnLoad: TButton;
    btnSave: TButton;
    procedure btnLoadClick(Sender: TObject);
    procedure btnSaveClick(Sender: TObject);
  private
    fInitialized: boolean;
    fPerson: TPerson;
    procedure Initialize;
    { Private declarations }
  public
    { Public declarations }
    procedure AfterConstruction; override;
    procedure BeforeDestruction; override;
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.AfterConstruction;
begin
  inherited;
  Initialize;
end;

procedure TForm1.BeforeDestruction;
begin
  fPerson.Free;
  inherited;
end;

procedure TForm1.btnLoadClick(Sender: TObject);
begin
  fPerson.Name := 'Doogie Howser';
  fPerson.Age := 15;
  BindScope1.DataObject := fPerson;
end;

procedure TForm1.btnSaveClick(Sender: TObject);
begin
  BindingsList1.Notify(Edit1, '');
end;

procedure TForm1.Initialize;
var
  expression: TBindExpression;
begin
  //Create a binding expression.
  expression := TBindExpression.Create(self);
  expression.ControlComponent := Edit1;
  expression.ControlExpression := 'Text';
  //The Text property of Edit1 ...
  expression.SourceComponent := BindScope1;
  expression.SourceExpression := 'Name';
  //... is bound to the Name property of fPerson
  expression.Direction := TExpressionDirection.dirBidirectional;
  //Add the expression to the bindings list.
  expression.BindingsList := BindingsList1;
  //Create a Person object.
  fPerson := TPerson.Create;
end;

{ TPerson }

procedure TPerson.SetName(const Value: string);
begin
  fName := Value;
  ShowMessage('Name changed to "'+ Value +'"');
end;

end.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值