Hi,
I am trying to use the DataGrid for the first time. I have dragged it onto my form, and as suggested in the docs I added the columns into the Data Grid’s column property editor as follows:
I’ve been struggling to get anything to populate at all in this datagrid though. Copying the example from the docs, I tried to hardcode the data to see if I could make anything happen:
self.tradeList.items = [{"Trade_Details":"123", "Price":"","Notional":"","Date":""}]
This still doesn’t result in anything populating. I would have expected a blank row, except that 123 at the start…
Any help is appreciated! Thanks
You should use square brackets to define a list of dictionaries:
self.tradeList.items = [...]
Thanks - I did try that and still no luck. I had that originally, but the Anvil docs had the “( )” in their example somewhere…
I have just tried and it works.
If you use normal parenthesis with more than one dict or with one dict followed by a comma, then you would make a tuple. I tried with a tuple and it doesn’t work.
If you use normal parenthesis with one dict as in the snippet, then it’s just a dict and it doesn’t work.
Where does the docs use “( )”?
I’ll have to find it… were you able to get my code to run though? Even with square brackets I can’t get it to run. I’ve just tried to bind RowTemplate1attributes to the self.item attributes, but thats now throwing a new error (progress I guess though…)
TypeError: string indices must be integers, not str
at Form1.RowTemplate1, line 14
called from Form1, line 80
called from Form1, line 21
I did.
Is self.tradeList
the datagrid or the repeating panel inside it?
You need to assign it to the repeating panel, not to the datagrid.
Yeah, I assigned it to the repeating panels .items attribute…
I seem to make some progress when I put labels on the RowTemplate6 form designer (vs the default blank) and then setting the binding in the init phase.
class RowTemplate6(RowTemplate6Template):
def __init__(self, **properties):
# Set Form properties and Data Bindings.
self.init_components(**properties)
# Any code you write here will run when the form opens.
self.Trade_Details.text = self.item["Trade_Details"]
but for some reason it seems to double up the rows.
what it looks like when it runs:
Hmm… Looks like duplicate items.
- What does
RowTemplate6
look like, when opened as its own Form, outside of the grid?
- Does the grid also contain an earlier version of
RowTemplate6
(e.g, RowTemplate1
, RowTemplate2
, …)
With those duplicates gone, does the grid look okay, now, in the Editor?
This doesn’t seem to be duplicated.
This seems to have the normal row above, with 4 columns with their values (in this case only 1 value), plus a component added to the template while not working in the inline editor, which causes the component not to be linked to any of the columns and to appear under the columns.
Template forms used on a datagrid should only be edited inside the datagrid to avoid this kind of problems (unless this is exactly what you are looking for).
How do you edit “inside the datagrid” ? I just saw the RowTemplate6 thing pop up, so thats where I edited it…
You double click on the graphic editor that contains the datagrid.
This will start the editing of the form in an environment that knows about the columns of the datagrid it belongs to.
If you edit it by clicking on the browser on the left, it doesn’t know it is going to be used as a template of a datagrid, so any component you add is not linked to any column.
got you, thanks for the help.
If I wanted to do something more complicated in the way that the data is displayed, any suggestions? My target data structure is something like below. It is a list, of lists of dictionaries. The 2nd internal list is usually 1 - 3 items long (they’re related entries), so I was hoping to display them as one ‘line’ in this datagrid.
{'0': [{'Trade_Details': '1x2s 3.170',
'Price': '153',
'Notional': '450',
'Date': '27-Jun-22'},
{'Trade_Details': '1x2s 0.000',
'Price': '152.80',
'Notional': '250',
'Date': '27-Jun-22'}],
'1': [{'Trade_Details': '5x7s 2.880',
'Price': '386.50',
'Notional': '460',
'Date': '27-Jun-22'},
{'Trade_Details': '5x7s 0.000',
'Price': '386.50',
'Notional': '250',
'Date': '27-Jun-22'}],
'2': [{'Trade_Details': '5x7s 2.880',
'Price': '386.50',
'Notional': '460',
'Date': '27-Jun-22'}]}
You can have the “main” values displayed on the columns of the template, then add a child datagrid and add the list to the items of the child datagrid’s repeating panel.
Of you could add a label or richtext inside one of the columns or under the columns, and put your list in there.
You could also play with click events and have the child list expand/collapse, but that’s another story.
1 Like
Got you - I’ll have to have a play. Thanks a lot. One last question. Any idea why this is spaced so weirdly…
Maybe you have some components in the template that are not included in columns?
For a simple case without any formatting, the template should work while being completely empty.
Then you can start adding the columns that require some special formatting or other components with special behavior.
1 Like