unit frmPersonsGrid;
interface
uses
System.SysUtils, System.Types, System.UITypes, System.Rtti, System.Classes,
System.Variants, FMX.Types, FMX.Controls, FMX.Forms, FMX.Dialogs,
FMX.TMSBaseControl, FMX.TMSGridCell, FMX.TMSGridOptions, FMX.TMSGridData,
FMX.TMSCustomGrid, FMX.TMSGrid, FMX.Objects, Data.Bind.EngExt,
FMX.Bind.DBEngExt, FMX.TMSGridDataBinding, System.Bindings.Outputs,
FMX.Bind.Editors, FMX.Layouts, FMX.Bind.Navigator, Data.Bind.Components,
Data.Bind.Grid, Data.Bind.DBScope, Data.Bind.Controls;
type
TFormPersonsGrid =
class(TForm)
TMSFMXGridPersons: TTMSFMXGrid;
Body: TRectangle;
BindNavigatorPersons: TBindNavigator;
Button1: TButton;
procedure TMSFMXGridPersonsGetCellReadOnly(Sender: TObject; ACol, ARow: Integer;
var AReadOnly: Boolean);
procedure TMSFMXGridPersonsCellCheckBoxClick(Sender: TObject; ACol, ARow: Integer; Cell: TFmxObject);
procedure TMSFMXGridPersonsCellEditDone(Sender: TObject; ACol, ARow: Integer; CellEditor: TFmxObject);
procedure TMSFMXGridPersonsGetCellProperties(Sender: TObject; ACol, ARow: Integer; Cell: TFmxObject);
procedure TMSFMXGridPersonsGetCellData(Sender: TObject; ACol, ARow: Integer;
var CellString:
string);
private
FillFlag: Boolean;
public
procedure FillGridData(Id: Integer = 0);
procedure SaveGridData;
end;
var
FormPersonsGrid: TFormPersonsGrid;
implementation
{$R *.fmx}
uses dFlyMagicM, Data.SqlExpr;
{ TFormPersonsGrid }
procedure TFormPersonsGrid.FillGridData(Id: Integer = 0);
var
DS: TSQLDataSet;
Grid: TTMSFMXGrid;
C: Integer;
R: Integer;
begin
FillFlag := True;
DS := DataModuleFlyMagicM.PERSONS;
Grid := TMSFMXGridPersons;
DS.Close;
DS.Open;
Grid.ColumnCount := 4 + 1;
Grid.RowCount := DS.RecordCount + 1;
Grid.ColumnWidths[1] := 0;
Grid.AddCheckBoxColumn(2);
for C := 1
to DS.Fields.Count
do
begin
Grid.Cells[C, 0] := DS.Fields[C - 1].FieldName;
end;
for R := 1
to Grid.RowCount
do
begin
if DS.FieldByName('
Id').AsInteger = Id
then
Grid.FocusedCell := Grid.SingleCell(Grid.FocusedCell.Col, R);
for C := 1
to DS.Fields.Count
do
begin
Grid.Cells[C, R] := DS.Fields[C - 1].AsString;
end;
DS.Next;
end;
DS.Close;
FillFlag := False;
end;
procedure TFormPersonsGrid.SaveGridData;
begin
end;
procedure TFormPersonsGrid.TMSFMXGridPersonsCellCheckBoxClick(Sender: TObject; ACol, ARow: Integer; Cell: TFmxObject);
var
B: Boolean;
DS: TSQLDataSet;
Grid: TTMSFMXGrid;
Id: Integer;
FieldName:
string;
Value:
string;
begin
if FillFlag
then
Exit;
if (ARow > 0)
and (ACol = 2)
then
begin
if Cell
is TTMSFMXCheckGridCell
then
begin
B := (Cell
as TTMSFMXCheckGridCell).CheckBox.IsChecked;
DS := DataModuleFlyMagicM.PersonsUpdate;
Grid := TMSFMXGridPersons;
Id := StrToIntDef(Grid.Cells[1, ARow], 0);
FieldName := Grid.Cells[ACol, 0];
Grid.FocusedCell := Grid.SingleCell(ACol, ARow);
if B
then
Value := '
1'
else
Value := '
0';
DS.Close;
DS.CommandText := '
UPDATE Persons SET ' + FieldName + '
= :Value WHERE Id = :Id';
DS.ParamByName('
Id').AsInteger := Id;
DS.ParamByName('
Value').AsString := Value;
DS.ExecSQL();
DS.Close;
FillGridData(Id);
end;
end;
end;
procedure TFormPersonsGrid.TMSFMXGridPersonsCellEditDone(Sender: TObject; ACol, ARow: Integer; CellEditor: TFmxObject);
var
DS: TSQLDataSet;
Grid: TTMSFMXGrid;
Id: Integer;
FieldName:
string;
Value:
string;
begin
DS := DataModuleFlyMagicM.PersonsUpdate;
Grid := TMSFMXGridPersons;
Id := StrToIntDef(Grid.Cells[1, ARow], 0);
FieldName := Grid.Cells[ACol, 0];
Value := Grid.Cells[ACol, ARow];
DS.Close;
DS.CommandText := '
UPDATE Persons SET ' + FieldName + '
= :Value WHERE Id = :Id';
DS.ParamByName('
Id').AsInteger := Id;
DS.ParamByName('
Value').AsString := Value;
DS.ExecSQL();
DS.Close;
FillGridData(Id);
end;
procedure TFormPersonsGrid.TMSFMXGridPersonsGetCellData(Sender: TObject; ACol, ARow: Integer;
var CellString:
string);
var
C: TFmxObject;
begin
if (ARow > 0)
and (ACol = 2)
then
begin
C := TMSFMXGridPersons.CellControls[ACol, ARow];
if C
is TTMSFMXCheckGridCell
then
(C
as TTMSFMXCheckGridCell).CheckBox.IsChecked := (CellString = '
1');
CellString := '
';
end;
end;
procedure TFormPersonsGrid.TMSFMXGridPersonsGetCellProperties(Sender: TObject; ACol, ARow: Integer; Cell: TFmxObject);
begin
if Cell
is TTMSFMXCheckGridCell
then
(Cell
as TTMSFMXCheckGridCell).CheckBox.IsChecked := (TMSFMXGridPersons.Cells[ACol, ARow] = '
1');
end;
procedure TFormPersonsGrid.TMSFMXGridPersonsGetCellReadOnly(Sender: TObject; ACol, ARow: Integer;
var AReadOnly: Boolean);
begin
AReadOnly := (ACol
in [1, 2]);
end;
end.