I want to create a component for adding data to a data grid (similar to this tutorial). However, I need to add each text box programmatically, since the columns are dynamic.
I am using code that looks like this:
drp = DataRowPanel()
for i in range(num_columns):
drp.add_component(TextBox(), column=i)
self.project_experiments_grid.add_component(drp)
While this is totally doable, if each row contains the same set of elements, you may consider exploring RepeatingPanels and their use within DataGrids (see the docs above).
Lastly, I believe you are referring to DataGrid components rather than DataTables (Anvil’s built in database service). I’ve edited the title and post accordingly, but please feel free to correct it if I have misunderstood.
By default, the ID of a Data Grid column is a random string, so you need to get the ID out of the Data Grid’s columns property. Your code will work if you change it to this:
drp = DataRowPanel()
for col in self.project_experiments_grid.columns:
drp.add_component(TextBox(), column=col["id"])
self.project_experiments_grid.add_component(drp)
You can designate your own column IDs if you set your columns in code, such as