1

I need to make the TCheckColumn from the FMX.StringGrid to work from an integer value but I don't know how. My code reads from a JSON request and translates it to a stringgrid. In the database the "boolean" field is stored as integer, so 0 for false and 1 for true. This is the code that reads from the request:

procedure TDM.CarregaDados(aTable: string; aGrid: TStringGrid);
begin
  TThread.CreateAnonymousThread(
    procedure
    var
      str: string;
    begin
      aGrid.RowCount := 0;

      REST.Response := nil;
      REST.Resource := aTable;
      REST.Method := rmGET;
      REST.Params.ClearAndResetID;

      REST.Execute;
      RESTDSA.Response := REST.Response;
      RESTDSA.DataSet := RESTDS;
      RESTDSA.Active := true;

      TThread.Synchronize(nil,
        procedure
        var
          I: Integer;
        begin
          aGrid.BeginUpdate;
          while not RESTDS.Eof do
          begin
            aGrid.RowCount := aGrid.RowCount + 1;
            for I := 0 to RESTDS.FieldCount - 1 do
              aGrid.Cells[I, aGrid.RowCount - 1] := RESTDS.Fields.Fields
                [I].AsString;
            RESTDS.Next;
          end;
          aGrid.EndUpdate;
        end);
      REST.ClearBody;
      REST.Params.ClearAndResetID;
    end).Start;
end;

REST is the TRESTRequest component,
RESTDS is the TFDMemTable,
RESTDSA is the TRESTRequestDataSetAdapter component,
aGrid is a TStringGrid and
aTable is the endpoint resource.

What I wanna know is how I can tweak this code to make it work with TCheckColumn in my grid. Yes, Of course I have a TIntegerColumn, a TStringColumn and a TCheckColumn previously added to the grid.

This is an example JSON response:

[
  {
    "ID" : 1,
    "Descr" : "test",
    "ischeck" : 0
  },
  {
    "ID" : 2,
    "Descr" : "test",
    "ischeck" : 1
  }
]
Mobius one
  • 171
  • 3
  • 12

1 Answers1

0

Well I know It's late, but I am a newbie here, and it's the first time I use FMX.TStringGrid without Livebindings.

I found a solution to this problem, with my own data

procedure TCsv4Presta.StringGrid1CellClick(const Column: TColumn;
  const Row: Integer);
begin
case Column.Index of

  0 : begin // my checkboxcolumn
      StringGrid1.Cells[0,Row]:= BooltoStr(Not StrToBool(StringGrid1.Cells[0,Row]),true);
      Column.UpdateCell(Row); // important to refresh checkbox
      end;
end;
end; 

just a problem with this onclick, you have to manage click in the cell but not on the checkbox

So I can suggest you a code like

while not RESTDS.Eof do
  begin
    aGrid.RowCount := aGrid.RowCount + 1;
    for I := 0 to RESTDS.FieldCount - 1 do
       begin
        if aGrid.Columns[I] is TCheckBoxColumn then 
           begin 
            aGrid.Cells[I, aGrid.RowCount - 1] := BooltoStr(RESTDS.Fields.Fields
                [I].AsString='1',true) ;
             // Column.UpdateCell(Row); 
           end 
        else  aGrid.Cells[I, aGrid.RowCount - 1] := RESTDS.Fields.Fields
                [I].AsString;
       end;           
    RESTDS.Next;
   end;
SergeGirard
  • 261
  • 1
  • 2
  • 8