-1

i dont know how to deal with stringrids, i want to fill it with data, I've succeeded to do it with a listview this is my code to fill the listview..

var
 LJSONArray : TJSONArray;
 LItem: TListViewItem;
  I: Integer;
 begin
 LJSONArray := TJSONArray.Create;
 try
 BackendStorage1.Storage.QueryObjects('ShoppingList', [], LJSONArray);
ListView1.ClearItems;
for I := 0 to LJSONArray.Count-1 do
begin
  LItem := ListView1.Items.Add;
  LItem.Text := (LJSonArray.Items[I].GetValue<string>('item'));
end;
finally
LJSONArray.Free;
end;
end;
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
markkk
  • 31
  • 2
  • 7

1 Answers1

1

To add items to a TStringGrid, you have to set its RowCount property first and then use its Cells property to fill them in, eg:

var
  LJSONArray : TJSONArray;
  I: Integer;
begin
  LJSONArray := TJSONArray.Create;
  try
    BackendStorage1.Storage.QueryObjects('ShoppingList', [], LJSONArray);
    StringGrid1.RowCount := LJSONArray.Count;
    for I := 0 to LJSONArray.Count-1 do
    begin
      StringGrid1.Cells[0, I] := LJSonArray.Items[I].GetValue<string>('item');
    end;
  finally
    LJSONArray.Free;
  end;
end;

Make sure you have set the grid's ColumnCount property to at least 1 beforehand, such as at design-time.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • error FixedRows undeclared identifier !! @Remy Lebeau – markkk Aug 21 '15 at 13:59
  • @markkk: VCL's `TStringGrid` has a published `FixedRows` property. Are you using FireMonkey instead of VCL? FireMonkey's `TStringGrid` does not have a `FixedRows` property, but it does have `RowCount` and `Cells` properties, so just remove the `FixedRows` references if you are indeed using FireMonkey. And please, next time, be more clear about which framework you are actually using. – Remy Lebeau Aug 21 '15 at 17:50
  • yes, im using firemonkey, and plz edit your answer, many thanks in advance, i'll be more specific nxt time @Remy Lebeau – markkk Aug 21 '15 at 19:10
  • @markkk: If you're using FireMonkey, you should edit your question to add that tag, so people know you're not asking about the VCL TStringGrid. We shouldn't have to guess which one you're using. – Ken White Aug 21 '15 at 19:26
  • @Remy: I see that (now). :-) Still a good point for the OP in the future. – Ken White Aug 21 '15 at 20:59
  • error again, Coumn index, 0, out of bounds @RemyLebeau – markkk Aug 21 '15 at 21:06
  • 1
    @markkk: Have you defined any columns in your grid? Have you even looked at the available properties of `TStringGrid`, or read its documentation? – Remy Lebeau Aug 21 '15 at 22:02
  • manged to do it, its works like charm, many thank Mr. @Remy Lebeau – markkk Aug 22 '15 at 14:06