unit Unit1;
interface
uses
Data.Bind.EngExt, Fmx.Bind.DBEngExt, FMX.TMSGridDataBinding, System.Rtti, System.Bindings.Outputs,
Fmx.Bind.Editors, Data.Bind.Components, Data.Bind.Grid, Data.Bind.ObjectScope, System.Classes,
FMX.Types, FMX.Controls, FMX.TMSBaseControl, FMX.TMSGridCell, FMX.TMSGridOptions, FMX.TMSGridData,
FMX.TMSCustomGrid, FMX.TMSGrid, FMX.Forms, System.Generics.Collections, FMX.Dialogs, FMX.ListBox,
System.SysUtils;
type
TPerson = class
private
FName: string;
FFirstName: string;
FGender: string;
FBirthday: TDateTime;
public
property Name: string read FName write FName;
property FirstName: string read FFirstName write FFirstName;
property Gender: string read FGender write FGender;
property Birthday: TDateTime read FBirthday write FBirthday;
constructor Create(N, F, G: string; B: TDateTime);
end;
TForm1 = class(TForm)
TMSFMXGrid1: TTMSFMXGrid;
AdapterBindSource1: TAdapterBindSource;
DataGeneratorAdapter1: TDataGeneratorAdapter;
BindingsList1: TBindingsList;
LinkGridToDataSourceAdapterBindSource: TLinkGridToDataSource;
procedure AdapterBindSource1CreateAdapter(Sender: TObject;
var ABindSourceAdapter: TBindSourceAdapter);
procedure FormDestroy(Sender: TObject);
procedure TMSFMXGrid1GetCellEditorType(Sender: TObject; ACol, ARow: Integer;
var CellEditorType: TTMSFMXGridEditorType);
procedure TMSFMXGrid1GetCellEditorProperties(Sender: TObject; ACol, ARow: Integer;
CellEditor: TFmxObject);
private
FPersonList: TObjectList<TPerson>;
procedure InitFPersonList;
procedure AfterPostPerson(Adapter: TBindSourceAdapter);
public
end;
var
Form1: TForm1;
implementation
{$R *.fmx}
const
COL_NAME = 0;
COL_FIRSTNAME = COL_NAME + 1;
COL_GENDER = COL_FIRSTNAME + 1;
COL_BIRTHDAY = COL_GENDER + 1;
// Values used in combobox
MALE_STR = 'Male';
FEMALE_STR = 'Female';
constructor TPerson.Create(N, F, G: string; B: TDateTime);
begin
FName:= N;
FFirstName:= F;
FGender:= G;
FBirthday:= B;
end;
// create the connection between the adapter bind source connected to the grid, and a list of person.
procedure TForm1.AdapterBindSource1CreateAdapter(Sender: TObject;
var ABindSourceAdapter: TBindSourceAdapter);
begin
InitFPersonList;
ABindSourceAdapter:= TListBindSourceAdapter<TPerson>.Create(Self, FPersonList, False);
ABindSourceAdapter.AfterPost:= AfterPostPerson;
end;
// uses to show a Person's stored values after editing
procedure TForm1.AfterPostPerson(Adapter: TBindSourceAdapter);
begin
if Assigned(Adapter.Current) then
with Adapter.Current as TPerson do
ShowMessage(FirstName + ' ' + Name + ' - ' + Gender + ' - ' + DateToStr(BirthDay));
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
if (Assigned(FPersonList)) then FPersonList.Free;
end;
// create a sample list of person, shown in the grid
procedure TForm1.InitFPersonList;
begin
if (Assigned(FPersonList)) then FPersonList.Free;
FPersonList:= TObjectList<TPerson>.Create;
FPersonList.Add(TPerson.Create('Dupond', 'Jean', MALE_STR, EncodeDate(1985, 1, 13)));
FPersonList.Add(TPerson.Create('Simon', 'Marie', FEMALE_STR, EncodeDate(1999, 3, 20)));
end;
procedure TForm1.TMSFMXGrid1GetCellEditorProperties(Sender: TObject; ACol, ARow: Integer;
CellEditor: TFmxObject);
begin
// initialize combobox list of values
if (ACol = COL_GENDER) then
with CellEditor as TComboBox do
begin
Items.Clear;
Items.Add(MALE_STR);
Items.Add(FEMALE_STR);
end;
end;
procedure TForm1.TMSFMXGrid1GetCellEditorType(Sender: TObject; ACol, ARow: Integer;
var CellEditorType: TTMSFMXGridEditorType);
begin
// define custom editors
case ACol of
COL_GENDER: CellEditorType:= TTMSFMXGridEditorType.etComboBox;
COL_BIRTHDAY: CellEditorType:= TTMSFMXGridEditorType.etDatePicker;
end;
end;
end.
// end of .pas file
fmx file :
object Form1: TForm1
Left = 0
Top = 0
Caption = 'Form1'
ClientHeight = 480
ClientWidth = 739
FormFactor.Width = 320
FormFactor.Height = 480
FormFactor.Devices = [dkDesktop, dkiPhone, dkiPad]
OnDestroy = FormDestroy
DesignerMobile = False
DesignerWidth = 0
DesignerHeight = 0
DesignerDeviceName = ''
DesignerOrientation = 0
DesignerOSVersion = ''
object TMSFMXGrid1: TTMSFMXGrid
Align = alClient
DisableFocusEffect = False
Height = 464.000000000000000000
Margins.Left = 8.000000000000000000
Margins.Top = 8.000000000000000000
Margins.Right = 8.000000000000000000
Margins.Bottom = 8.000000000000000000
TabOrder = 0
Width = 723.000000000000000000
DefaultRowHeight = 24.000000000000000000
DefaultColumnWidth = 100.000000000000000000
FixedColumns = 0
ColumnCount = 0
RowCount = 201
Options.Editing.CalcFormat = '%g'
Options.Grouping.CalcFormat = '%g'
Options.Grouping.GroupCountFormat = '(%d)'
Options.IO.XMLEncoding = 'ISO-8859-1'
Options.Mouse.TouchScrollingSensitivity = 0.100000001490116100
Options.Printing.PageNumberFormat = '%g'
Version = '2.2.0.4'
Fill.Kind = bkSolid
Stroke.Kind = bkSolid
StrokeThickness = 1.000000000000000000
TopRow = 0
LeftCol = 0
OnGetCellEditorType = TMSFMXGrid1GetCellEditorType
OnGetCellEditorProperties = TMSFMXGrid1GetCellEditorProperties
Columns = <
item
BorderWidth = 1.000000000000000000
Color = claNull
Editor = etEdit
Fixed = False
FixedFont.Style = [fsBold]
FixedFontColor = claBlack
FontColor = claBlack
ID = ''
Width = 100.000000000000000000
end
item
BorderWidth = 1.000000000000000000
Color = claNull
Editor = etEdit
Fixed = False
FixedFont.Style = [fsBold]
FixedFontColor = claBlack
FontColor = claBlack
ID = ''
Width = 100.000000000000000000
end
item
BorderWidth = 1.000000000000000000
Color = claNull
Editor = etEdit
Fixed = False
FixedFont.Style = [fsBold]
FixedFontColor = claBlack
FontColor = claBlack
ID = ''
Width = 100.000000000000000000
end
item
BorderWidth = 1.000000000000000000
Color = claNull
Editor = etEdit
Fixed = False
FixedFont.Style = [fsBold]
FixedFontColor = claBlack
FontColor = claBlack
ID = ''
Width = 100.000000000000000000
end
item
BorderWidth = 1.000000000000000000
Color = claNull
Editor = etEdit
Fixed = False
FixedFont.Style = [fsBold]
FixedFontColor = claBlack
FontColor = claBlack
ID = ''
Width = 100.000000000000000000
end>
end
object AdapterBindSource1: TAdapterBindSource
AutoActivate = True
OnCreateAdapter = AdapterBindSource1CreateAdapter
Adapter = DataGeneratorAdapter1
ScopeMappings = <>
Left = 536
Top = 360
end
object DataGeneratorAdapter1: TDataGeneratorAdapter
FieldDefs = <>
Active = True
AutoPost = False
Options = [loptAllowInsert, loptAllowDelete, loptAllowModify]
Left = 536
Top = 416
end
object BindingsList1: TBindingsList
Methods = <>
OutputConverters = <>
Left = 20
Top = 5
object LinkGridToDataSourceAdapterBindSource: TLinkGridToDataSource
Category = 'Liaisons rapides'
DataSource = AdapterBindSource1
GridControl = TMSFMXGrid1
AutoBufferCount = False
Columns = <>
end
end
end