05-16-2012، 09:12 AM
استفاده از این قابلیت جالب در دلفی XE2 که واقعا کار برنامه نویس رو خیلی راحت میکنه و براحتی میتونین یه لیست بسازین و بایند کنید:
اینم یه مثال راحتتر که بهتر متوجه بشین از کارکرد اون :
کد:
unit Unit15;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.Grids, System.Generics.Collections;
type
TPerson = class(TObject)
private
FLastName: String;
FFirstName: string;
published
property firstname : string read FFirstName write FFirstName;
property Lastname : String read FLastName write FLastName;
end;
TForm15 = class(TForm)
StringGrid1: TStringGrid;
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
Data : TList<TPerson>;
end;
var
Form15: TForm15;
implementation
{$R *.dfm}
procedure TForm15.FormCreate(Sender: TObject);
var
P : TPerson;
begin
Data := TList<TPerson>.Create;
P := TPerson.Create;
P.firstname := 'John';
P.Lastname := 'Doe';
Data.Add(P);
P := TPerson.Create;
P.firstname := 'Jane';
P.Lastname := 'Doe';
Data.Add(P);
// What can I add here or in the designer to link this to the TStringGrid.
end;
end.
کد:
procedure TForm15.FormCreate(Sender: TObject);
var
P : TPerson;
bgl: TBindGridList;
bs: TBindScope;
colexpr: TColumnFormatExpressionItem;
cellexpr: TExpressionItem;
begin
Data := TList<TPerson>.Create;
P := TPerson.Create;
P.firstname := 'John';
P.Lastname := 'Doe';
Data.Add(P);
P := TPerson.Create;
P.firstname := 'Jane';
P.Lastname := 'Doe';
Data.Add(P);
// What can I add here or in the designer to link this to the TStringGrid.
while StringGrid1.ColumnCount<2 do
StringGrid1.AddObject(TStringColumn.Create(self));
bs := TBindScope.Create(self);
bgl := TBindGridList.Create(self);
bgl.ControlComponent := StringGrid1;
bgl.SourceComponent := bs;
colexpr := bgl.ColumnExpressions.AddExpression;
cellexpr := colexpr.FormatCellExpressions.AddExpression;
cellexpr.ControlExpression := 'cells[0]';
cellexpr.SourceExpression := 'current.firstname';
colexpr := bgl.ColumnExpressions.AddExpression;
cellexpr := colexpr.FormatCellExpressions.AddExpression;
cellexpr.ControlExpression := 'cells[1]';
cellexpr.SourceExpression := 'current.lastname';
bs.DataObject := Data;
end;
اینم یه مثال راحتتر که بهتر متوجه بشین از کارکرد اون :
کد:
type
TPerson = class(TObject)
private
Number: Integer;
MName: String;
MFamily: string;
MNationalCode: string;
MBCNumber: string;
MBirthday: string;
MSex: string;
MRelation: string;
published
property ID: string read Number write Number;
property firstname: string read MFamily write MFamily;
property Lastname: String read MName write MName;
property NationalCode: String read MNationalCode write MNationalCode;
property BCNumber: string read MBCNumber write MBCNumber;
property Birthday: String read MBirthday write MBirthday;
property s /\/\///\e///:/x: string read MSex write MSex;
property Relation: String read MRelation write MRelation;
{ +++++++++++++++++++++++++++++++ }
private
{ Pr!v8 var }
{ +++++++++++++++++++++++++++++++ }
public
end;
procedure BindingListMember( Data : TList<TPerson>);
var
bgl: TBindGridList;
bs: TBindScope;
colexpr: TColumnFormatExpressionItem;
cellexpr: TExpressionItem;
begin
// while StringGrid1.ColCount<2 do
// StringGrid1.AddObject(TStringColumn.Create(self));
bs := TBindScope.Create();
bgl := TBindGridList.Create();
bgl.ControlComponent := Form1.StringGrid1;
bgl.SourceComponent := bs;
colexpr := bgl.ColumnExpressions.AddExpression;
cellexpr := colexpr.FormatCellExpressions.AddExpression;
cellexpr.ControlExpression := 'cells[0]';
cellexpr.SourceExpression := 'current.firstname';
colexpr := bgl.ColumnExpressions.AddExpression;
cellexpr := colexpr.FormatCellExpressions.AddExpression;
cellexpr.ControlExpression := 'cells[1]';
cellexpr.SourceExpression := 'current.lastname';
bs.DataObject := Data;
end;
{ ------------------------------------------------------------------------------------------- }
procedure SetFromAdoQuery(Query : string);
var
I: Integer;
P: TPerson;
Data: TList<TPerson>;
begin
{ +++++++++++++++++++++++++++++++++ }
with DataModule2.qryMember do
begin
Close;
SQL.Clear;
SQL.Add(Query);
Open;
end;
Data := TList<TPerson>.Create;
for I := 1 to DataModule2.qryMember.Eof do
begin
P := TPerson.Create;
with DataModule2 do
begin
p.Number:= I;
p.MName:= qryMemberPName.AsString;
p.MFamily := qryMemberPFamily.AsString;
p.MNationalCode:= qryMemberNationalCode.AsString;
p.MBCNumber := qryMemberBCNember.AsString;
p.MBirthday := qryMemberBirthday.AsString;
p.MSex:= qryMemberSex.AsString;
p.MRelation:= qryMemberRelation.AsString;
end;
Data.Add(P);
end;
BindingListMember(P,Data);
end;