• ¡Welcome to Square Theme!
  • This news are in header template.
  • Please ignore this message.
مهمان عزیز خوش‌آمدید. ورود عضــویت


امتیاز موضوع:
  • 19 رای - 2.58 میانگین
  • 1
  • 2
  • 3
  • 4
  • 5
Title: LiveBinding در TStringGrid - Delphi XE2
حالت موضوعی
#1
استفاده از این قابلیت جالب در دلفی 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;
معبودا مرا ببخش، بخاطر درهایی که کوبیدم ولی هیچکدام خانه تو نبود ...
 
پاسخ
  


موضوعات مشابه ...
موضوع نویسنده پاسخ بازدید آخرین ارسال
  Delphi XE3 Saeed7007 5 7,536 09-10-2012، 08:30 PM
آخرین ارسال: Amin_Mansouri
  app inside Delphi Saeed7007 7 7,615 04-25-2012، 11:24 PM
آخرین ارسال: Amin_Mansouri
  Delphi 7 Lite Full Edition v3.0.7.6 Amin_Mansouri 0 3,473 01-31-2012، 05:29 PM
آخرین ارسال: Amin_Mansouri
  Write 64 Bit Program Delphi Amin_Mansouri 1 5,004 10-24-2011، 04:58 PM
آخرین ارسال: Amin_Mansouri
  RE: Delphi XE2 به زودی منتشر می شود Amin_Mansouri 1 4,448 09-13-2011، 10:57 AM
آخرین ارسال: Amin_Mansouri
  source code hook (Delphi) Amin_Mansouri 0 3,266 05-22-2011، 10:34 AM
آخرین ارسال: Amin_Mansouri
  Source Code WinPCap Delphi Amin_Mansouri 0 4,536 04-24-2011، 01:14 PM
آخرین ارسال: Amin_Mansouri
  Delphi Source Code and Tutorials Amin_Mansouri 2 5,065 04-16-2011، 02:09 PM
آخرین ارسال: Amin_Mansouri

پرش به انجمن:


Browsing: 1 مهمان