DataGrid from code - properties not working?

What I’m trying to do:
Build a DataGrid from code (instead of the UI drag-n-drop), and I’m having trouble getting some of the property settings don’t seem to work. Specifically, show_page_controls=True won’t display, and role=‘wide’ won’t scroll horizontally (from here)

What I’ve tried and what’s not working:
I setup a test page with a DataGrid setup from the UI and another setup from code. Tried applying the property settings to both - works in the UI version:
self.data_grid_test2.show_page_controls = True
self.data_grid_test2.show_page_controls = 20
self.data_grid_test2.role = ‘wide’

and not in the code version:
grid = DataGrid()
grid.show_page_controls = True
grid.rows_per_page = 20
grid.role = ‘wide’

thanks,
chris

There’s a few other things youll have to do in code.

You’ll need to also set the columns of the datagrid.

# the columns from the example you're referring to

[{'width': '100', 'title': 'A very long column name 1', 'id': 'ATZEXP', 'data_key': 'column_1', 'expand': False},
{'width': '100', 'title': 'A very long column name 2', 'id': 'EBSLZQ', 'data_key': 'column_2', 'expand': False}, 
{'width': '100', 'title': 'A very long column name 3', 'id': 'ZTXTBS', 'data_key': 'column_3', 'expand': False},

...

The ids should be unique.

You’ll also want to add a repeating panel to the datagrid in code.
Set the item_template of the repeating panel and set the items property.
(the item_template can just be a DataRowPanel)

This section of the docs should help:

Anvil Docs | Data Grids

1 Like