Delphi XE2의 FireMonkey 시작(31) - 데이터 바인딩: 데이터베이스 바인딩

3859 단어 Delphi
1. 전체 설계 시 조작:
먼저 창에 컨트롤을 놓습니다.

DataSource1    : TDataSource;
ClientDataSet1 : TClientDataSet;
Label1         : TLabel;
Edit1          : TEdit;
Memo1          : TMemo;
ImageControl1  : TImageControl;
BindNavigator1 : TBindNavigator;

{      ,          }
BindingsList1               : TBindingsList;
BindScopeDB1                : TBindScopeDB;

DBLinkLabel1SpeciesNo1      : TBindDBTextLink;
DBLinkEdit1Category1        : TBindDBEditLink;
DBLinkMemo1Notes1           : TBindDBMemoLink;
DBLinkImageControl1Graphic1 : TBindDBImageLink;
테스트는 정부에서 제공한 테스트 데이터biolife를 사용합니다.xml(또는biolife.cds)의 경로는 보통 C:\Program Files\Common Files\CodeGear Shared\Data\biolife입니다.xml
연결 단계:

1、  DataSource1   DataSet     ClientDataSet1;

2、  ClientDataSet   FileName     'C:\Program Files\Common Files\CodeGear Shared\Data\biolife.xml';

3、           biolife.xml     :
   Label1       ->    -> Link To DB Field... ->    Species No (  )
   Edit1        ->    -> Link To DB Field... ->    CateGory   (string)
   Memo1        ->    -> Link To DB Field... ->    Notes      (Text)
   ImageControl ->    -> Link To DB Field... ->    Graphic    (Graphics)

4、  BindNavigator1   BindScope     BindScopeDB1;

5、  ClientDataSet1   Active     True.
2. 런타임 연결:
먼저 창에 컨트롤을 배치합니다(다른 것은 런타임 시 완료).

DataSource1    : TDataSource;
ClientDataSet1 : TClientDataSet;
Label1         : TLabel;
Edit1          : TEdit;
Memo1          : TMemo;
ImageControl1  : TImageControl;
BindNavigator1 : TBindNavigator;
BindingsList1  : TBindingsList;
BindScopeDB1   : TBindScopeDB;
디자인할 때와 동일한 코드:

procedure TForm1.FormCreate(Sender: TObject);
begin
  ClientDataSet1.FileName := 'C:\Program Files\Common Files\CodeGear Shared\Data\biolife.xml';
  DataSource1.DataSet := ClientDataSet1;
  BindScopeDB1.DataSource := DataSource1;
  BindNavigator1.BindScope := BindScopeDB1;

  with TBindDBTextLink.Create(BindingsList1) do
  begin
    ControlComponent := Label1;
    DataSource := BindScopeDB1;
    FieldName := 'Species No';
  end;

  with TBindDBEditLink.Create(BindingsList1) do
  begin
    ControlComponent := Edit1;
    DataSource := BindScopeDB1;
    FieldName := 'Category';
  end;

  with TBindDBMemoLink.Create(BindingsList1) do
  begin
    ControlComponent := Memo1;
    DataSource := BindScopeDB1;
    FieldName := 'Notes';
  end;

  with TBindDBImageLink.Create(BindingsList1) do
  begin
    ControlComponent := ImageControl1;
    DataSource := BindScopeDB1;
    FieldName := 'Graphic';
  end;

  ClientDataSet1.Active := True;
end;
TstringGrid에 바인딩하려면 StringGrid 1 -> Link to DB DataSource...->BindScopeDB1
다음은 런타임 시 TstringGrid에 바인딩된 코드입니다.

uses Fmx.Bind.Editors, Data.Bind.DBLinks, Fmx.Bind.DBLinks;

procedure TForm1.FormCreate(Sender: TObject);
begin
  ClientDataSet1.FileName := 'C:\Program Files\Common Files\CodeGear Shared\Data\biolife.xml';
  DataSource1.DataSet := ClientDataSet1;
  BindScopeDB1.DataSource := DataSource1;
  BindNavigator1.BindScope := BindScopeDB1;
  ClientDataSet1.Active := True;

  with TBindDBGridLink.Create(BindingsList1) do //    TBindGridLink
  begin
     GridControl := StringGrid1;
     DataSource := BindScopeDB1;
     Active := True;
  end;
end;

좋은 웹페이지 즐겨찾기